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、生成效果如下:

最新文章

  1. 让DIV中文字换行显示
  2. webpack中字体配置,可以引入bootstrap
  3. python27 windows 下三种安装第三方库的办法
  4. Java集合系列:-----------01集合的整体框架
  5. DirectX的引用找不到问题
  6. 计算机病毒实践汇总二:bufferzone沙盘使用体验
  7. Tlist
  8. Ajax 无刷新在注册用户名时的应用的代码
  9. openerp经典收藏 深入理解报表运行机制(转载)
  10. OpengGL ES2.0 Using NDK
  11. js预处理图片个人见解1
  12. BZOJ 1009 :[HNOI2008]GT考试(KPM算法+dp+矩阵快速幂)
  13. shell:正则表达式和文本处理器
  14. iTextCharp c#
  15. 一台电脑安装两个JDK
  16. 中断MSI INTA
  17. 【转】Linux中的EAGAIN含义
  18. 使用eclipse阅读java源码
  19. [转][C#]单例模式之懒加载
  20. POJ 3281 Dining(最大流+拆点)

热门文章

  1. 南昌邀请赛网络赛 D.Match Stick Game(dp)
  2. supervisor 通过图形界面管理
  3. Selenium--使用参考
  4. centos7.2云主机安装桌面
  5. 已经菜到不行了 PAT 1010. Radix (25)
  6. Linux文件系统_每一个的意义
  7. ASP.NET配置文件Web.config 详细解释
  8. &lt;转&gt;Android APP字体大小,不随系统的字体大小变化而变化的方法
  9. PartyPlay发布版
  10. 边框画的三角形给shadow