添加依赖
package com.cyg.writeexcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public void writeExcel(){
String path="D:/";
String filePathName = null;
Long nowTime=System.currentTimeMillis();
String title = "导出管理表";
String fileName = title+nowTime;
String sourFile =path+"poi"+File.separatorChar+"exporttemplate"+File.separatorChar+"ymb.xls";//原模板
String destFile = path +"poi"+File.separatorChar+"xlsoutput"+File.separatorChar+nowTime; //复制到存放的目标位置
File file = new File(sourFile);
if(file.isFile()) {
File savefile = new File(destFile);
if (!savefile.exists()) {
savefile.mkdirs();
}
FileInputStream input =new FileInputStream(sourFile);
FileOutputStream output = new FileOutputStream(destFile+File.separatorChar+fileName+".xls");
int in = input.read();
while( in != -1) {
output.write(in);
in =input.read();
}
filePathName =destFile+File.separatorChar+fileName+".xls";
FileInputStream inFile=new FileInputStream(filePathName);
jxl.Workbook workbookA = jxl.Workbook.getWorkbook(inFile);
WritableWorkbook wwb = jxl.Workbook.createWorkbook(new File(filePathName),workbookA);
WritableSheet sheetA = wwb.getSheet(0);// 获取文件第一个sheet
Label labelA = null;
WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
8, WritableFont.NO_BOLD);// 字体样式
WritableCellFormat wcf = new WritableCellFormat(font);
wcf.setAlignment(jxl.format.Alignment.CENTRE);//平行居中
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中
wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle边框
//获取数据源
for (int i = 1; i < 1000; i++) {
labelA = new Label(0,i,i+" ",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(1,i,"20180101" + i,wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(2,i,"开发00"+i,wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(3,i,i+" ");
sheetA.addCell(labelA);
labelA = new Label(4,i,"20180101" + i,wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(5,i,i+" ",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(6,i,"的官方价格就感觉几个几个几个",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(7,i,"的官方价格就感觉几个几个几个",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(8,i,"的官方价格就感觉几个几个几个",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(9,i,"的官方价格就感觉几个几个几个",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(10,i,"的官方价格就感觉几个几个几个",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(11,i,"的官方价格就感觉几个几个几个",wcf);//列、行、值、样式
sheetA.addCell(labelA);
labelA = new Label(12,i,"的官方价格就感觉几个几个几个",wcf);//列、行、值、样式
sheetA.addCell(labelA);
}
output.flush();
output.close();
input.close();
wwb.write(); //写入数据
wwb.close();
workbookA.close(); //关闭连接
System.out.println("完成");
}
}
注:POI支持excel2003和2007,而jxl只支持excel2003。
如果有如下警告
Warning: Cannot read name ranges for _ T o c 521 -setting to empty
Warning: Usage of a local non-builtin name
解决方案:打开模板==》公式==》名称管理器==》删除里面表格的信息