您的位置:首页 > 其它

poi 导入excel源码

2012-12-13 11:45 381 查看
package excel;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

import means.ShowProduct;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.HSSFColor;

import domain.Product;

public class CreateExcel {

/**

* @author LIANAN

* @param list 给一个查询集合

*

*/

public static void createExcels(List<Product> list){

long t1 = new Date().getTime();
HSSFWorkbook workbook=new HSSFWorkbook(); //create a excel file
HSSFSheet sheet = workbook.createSheet(); //create a excel sheet
HSSFCellStyle cellstyle = workbook.createCellStyle(); //create a excel style
cellstyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index);// set excel style color
int temp=60000; //一个sheet装6万条数据
getRow(sheet, cellstyle); //装入表头
HSSFRow row=null;
HSSFSheet sheet1=workbook.createSheet();
int k=0;
for (int i = 0; i < list.size(); i++) {

if(i<=temp){

row=sheet.createRow(i+1);

}else if(i>temp){

getRow(sheet1, cellstyle);

row=sheet1.createRow(k++);

}

row.createCell(0,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(list.get(i).getId());

row.createCell(1,HSSFCell.CELL_TYPE_STRING).setCellValue(list.get(i).getKw());

row.createCell(2,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(list.get(i).getSta());

row.createCell(3,HSSFCell.CELL_TYPE_STRING).setCellValue(list.get(i).getIndate());

}

try {

SimpleDateFormat sim=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");

String date=sim.format(new Date());

FileOutputStream out = new FileOutputStream("E:/lianan"+date+".xls");

workbook.write(out);

out.flush();

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}
long t2=new Date().getTime();
System.out.println(t2-t1);//5S左右时间

}

public static HSSFRow getRow(HSSFSheet sheet,HSSFCellStyle cellstyle){

HSSFRow titleRow=sheet.createRow(0);

titleRow.createCell(0).setCellValue("id");
titleRow.createCell(1).setCellValue("kw");
titleRow.createCell(2).setCellValue("sta");
titleRow.createCell(3).setCellValue("indate");
titleRow.setRowStyle(cellstyle);

return titleRow;

}

public static void main(String arg0[]){

createExcels(ShowProduct.showProduct());

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: