首页 > 编程语言 > 详细

JAVA利用JXL导出 EXCEL (在原有的excel模板上把数据导到excel上)

时间:2019-06-17 15:09:10      阅读:129      评论:0      收藏:0      [点我收藏+]

添加依赖

<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>

 

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
解决方案:打开模板==》公式==》名称管理器==》删除里面表格的信息

 

JAVA利用JXL导出 EXCEL (在原有的excel模板上把数据导到excel上)

原文:https://www.cnblogs.com/run1/p/11039054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!