java吧 关注:1,295,925贴子:12,832,310

回复:自学贴,同道请进。

取消只看楼主收藏回复

import java.sql.*;
import java.io.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class shuchu{
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Imp imp=new Imp();
imp.Exc1();
imp=null;
System.gc();
}
}
class Imp{
@SuppressWarnings("resource")
public void Exc1(){
int pageNow=1;
int pageSize=1048576;
int i=0;
int k=0;
int sheets=0;
int count=0;
@SuppressWarnings("unused")
int pageCount=0;
String table="c_meter";
Connection ct=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
ct=DriverManager.getConnection("jdbc:oracle:thin:@LENOVO-PC:1521:orcl","sjhc","sjhc");
String sql="select * from (select t.*,rownum rn from (select * from c_meter) t where rownum<=?) where rn>=?";
//计算要导出的表共有多少列数据
int column=0;
ps=ct.prepareStatement("select max(column_id) from user_tab_columns where table_name=upper('c_meter')");
rs=ps.executeQuery();
while(rs.next()){
column=rs.getInt(1);
}
//计算要导出的表共有多少行数据
ps=ct.prepareStatement("select count(*) from c_meter");
rs=ps.executeQuery();
while(rs.next()){
count=rs.getInt(1);
}
//进行分页,每页为2007版Excel中sheet页最大存放行数,即1048576行数据
if(count%pageSize==0){
sheets=count/pageSize;
}else{
sheets=count/pageSize+1;
}
Workbook workbook = new SXSSFWorkbook(500);//每次缓存500条到内存,其余写到磁盘。
System.out.println(table+"表导出开始、、");
//循环导出Sheet页数据
for(k=0;k<sheets;k++){
int m=0;
int start = pageSize*(pageNow-1)+1;
int end = pageSize*pageNow;
ps=ct.prepareStatement(sql);
ps.setInt(1, end);
ps.setInt(2, start);
rs=ps.executeQuery();
Sheet sheet = workbook.createSheet("sheet"+(k+1));
System.out.println("正在导出sheet"+(k+1));
while(rs.next()){
Row xssfrow=sheet.createRow(m);
for(int j=0;j<column;j++){
Cell xssfcell0=xssfrow.createCell((short)j);
xssfcell0.setCellValue(rs.getString(j+1));
}
start++;
i++;
m++;
}
pageNow++;
}
FileOutputStream fOut = new FileOutputStream("C:/Users/lenovo/Desktop/"+table+".xlsx");
//FileOutputStream fileoutputstreamx=new FileOutputStream("C:/Users/lenovo/Desktop/西安计量箱表关系合并.xlsx");
workbook.write(fOut);
fOut.close();
System.out.println("导出"+i+"条数据成功、、");
System.gc();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ct!=null){
try {
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


52楼2015-09-23 23:32
回复
    大神都来看看了,多多指点


    53楼2015-09-23 23:33
    回复
      2026-01-27 00:18:20
      广告
      不感兴趣
      开通SVIP免广告
      水一贴也好啊


      来自Android客户端54楼2015-09-23 23:37
      回复
        唉哟哟哟


        来自Android客户端55楼2015-09-23 23:46
        回复
          大神都休息了


          来自Android客户端57楼2015-09-24 00:00
          回复