java批量生成excel文件
2024-08-26 02:00:01
1、导入用于操作excel的jar,地址:https://pan.baidu.com/s/1qXADRlU
2、生成excel使用的模版文件,地址:https://pan.baidu.com/s/1c2y1rIo
3、java代码如下:
package test.job.day1130; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelUtil {
private File createExcelFile(String path,String fileName)throws Exception{
InputStream in = null;
OutputStream out = null;
File excelFile = createNewFile(path,fileName);
//System.out.println(excelFile.getName());
//模版
File templateFile = new File(path+"/template","template.xls");
if(!templateFile.exists())
throw new Exception("模版文件不存在");
//System.out.println(templateFile.getName());
try{
in = new BufferedInputStream(new FileInputStream(templateFile),1024);
out = new BufferedOutputStream(new FileOutputStream(excelFile),1024);
byte[] buffer = new byte[1024];
int len;
while((len=in.read(buffer)) != -1){
out.write(buffer,0,len);
out.flush();
}
}finally{
if(in != null)
in.close();
if(out != null)
out.close();
}
return excelFile;
} /*初始化excel文件*/
private void initExcelFile(File excelFile,String prefix)throws Exception{
InputStream is = null;
OutputStream out = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null; is = new FileInputStream(excelFile); workbook = new HSSFWorkbook(is);
String suffix = "";
//获取第一个sheet
sheet = workbook.getSheetAt(0); if(sheet != null){
//写数据
for(int i=0;i<399;i++){
HSSFRow row = sheet.createRow(i);
HSSFCell cell = row.createCell(0); if(i == 0){
cell.setCellValue("帐号");
cell = row.createCell(1);
cell.setCellValue("密码");
continue;
} if(i < 10){
suffix = "00" + i;
}
else if(i < 100){
suffix = "0" + i;
}
else{
suffix = i + "";
}
cell.setCellValue(prefix + suffix);
cell = row.createCell(1);
cell.setCellValue("000000");
}
out = new FileOutputStream(excelFile);
workbook.write(out);
}
out.flush();
out.close(); } private File createNewFile(String path,String fileName)throws Exception{
File newFile = new File(path,fileName); if(!newFile.exists())
newFile.createNewFile(); return newFile;
} public static void main(String[] args)throws Exception{ String path = "d:/excelFiles";
String fileName = "";
String prefix = "";
String tmpStr = "";
//char[] charArr = {'A','B','C','D','E','F','G','H','I','J'};
char[] charArr = {'O','P','Q'};
long t0 = System.currentTimeMillis();
for(int i=0;i<charArr.length;i++){
for(int j=0;j<100;j++){
if(j<10){
tmpStr = "0" + j;
}else{
tmpStr = "" + j;
} prefix = charArr[i] + tmpStr;
fileName = "file" + prefix + ".xls";
ExcelUtil eu = new ExcelUtil();
System.out.println("正在创建 " + fileName + "文件..");
File f = eu.createExcelFile(path,fileName);
eu.initExcelFile(f,prefix);
}
}
long t1 = System.currentTimeMillis(); System.out.println("耗时:" + (t1-t0)/1000 + "秒钟"); // String fileName = "file000.xls";
// ExcelUtil eu = new ExcelUtil();
// File f = eu.createExcelFile(path,fileName);
// eu.initExcelFile(f,"a00");
}
}
4、生成效果如下:
最新文章
- 让DIV中文字换行显示
- webpack中字体配置,可以引入bootstrap
- python27 windows 下三种安装第三方库的办法
- Java集合系列:-----------01集合的整体框架
- DirectX的引用找不到问题
- 计算机病毒实践汇总二:bufferzone沙盘使用体验
- Tlist
- Ajax 无刷新在注册用户名时的应用的代码
- openerp经典收藏 深入理解报表运行机制(转载)
- OpengGL ES2.0 Using NDK
- js预处理图片个人见解1
- BZOJ 1009 :[HNOI2008]GT考试(KPM算法+dp+矩阵快速幂)
- shell:正则表达式和文本处理器
- iTextCharp c#
- 一台电脑安装两个JDK
- 中断MSI INTA
- 【转】Linux中的EAGAIN含义
- 使用eclipse阅读java源码
- [转][C#]单例模式之懒加载
- POJ 3281 Dining(最大流+拆点)