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());
}
}
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());
}
}
相关文章推荐
- poi 机制 效率最高的 excel 导入Oracle (源码)
- Java POI 导入Excel以及解析Excel
- [置顶] 【Excel_To_DB】SpringBoot+EasyPoi+Redis消息队列实现Excel批量异步导入数据库(三)
- 170313、poi:采用自定义注解的方式导入、导出excel(这种方式比较好扩展)
- POI 导入导出Excel文件到数据库(转载)
- 利用POI将数据库表结构信息导入excel模板中
- java的poi技术读取和导入Excel
- POI解析excel之----经典导入
- POI和Java Excel Api导入导出----详细到你不敢相信
- POI导入2007EXCEL中遇到的文件描述符错误的问题
- POI导入导出excel资源汇总
- POI操作excel的导入与导出
- Java+Poi 读取Excel做数据导入时获取cell值的方法
- 利用POI读入excel通过反射生成Bean源码实现
- poi导入导出excel文件,兼容.xls和.xlsx两种格式
- Java POI 导入Excel
- 记 springmvc使用POI,快速实现Excel导入导出
- 利用POI将数据表导入Excel
- web中使用POI导入导出EXCEL文件的例子
- POI 导入导出Excel文件到数据库(转载)