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

java批量生成excel文件的更多相关文章

  1. java批量生成excel代码分享

    package com.test.util; /** * @author ocq * */ import java.io.FileOutputStream; import java.io.IOExce ...

  2. XLSTransformer生成excel文件简单演示样例

    项目结构图: 项目中所用到的jar,能够到http://www.findjar.com/index.x下载 ExcelUtil类源代码: package util; import java.io.IO ...

  3. XLSTransformer生成excel文件

    jxls的使用方法: 1)声明一个XLSTransformer对象,生成方式就是使用new操作符                 XLSTransformer transformer = new XL ...

  4. 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件

    今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...

  5. springMVC(4)---生成excel文件并导出

    springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...

  6. java上传excel文件及解析

      java上传excel文件及解析 CreateTime--2018年3月5日16:25:14 Author:Marydon 一.准备工作 1.1 文件上传插件:swfupload: 1.2 文件上 ...

  7. java代码将excel文件中的内容列表转换成JS文件输出

    思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...

  8. FluentData-新型轻量级ORM 利用T4模板 批量生成多文件 实体和业务逻辑 代码

    FluentData,它是一个轻量级框架,关注性能和易用性. 下载地址:FlunenData.Model 利用T4模板,[MultipleOutputHelper.ttinclude]批量生成多文件 ...

  9. 如何生成excel文件作为图像识别结果

    如何生成excel文件作为图像识别结果 在进行大规模图像处理的时候,如果能够以表格的形式生成结果文件,将非常的直观.这个时候,选择excel作为结果输出文件,将是合适的. 查询相关资料,有很多关于ex ...

随机推荐

  1. Vuejs+axios+SpringMVC 1

    原文:https://blog.csdn.net/annghi/article/details/78925668 Vuejs+axios+SpringMVC4.3.7 (一) 2017年12月28日 ...

  2. Spyder使用IPython Console弹出绘图窗口的设置方法

    http://www.datastudy.cc/article/3cfc3aff3b2c5948b938456e00376276 在使用Spyder的过程中,因为它的绘图,默认是绘图在IPython窗 ...

  3. Python 爬虫入门(三)—— 寻找合适的爬取策略

    写爬虫之前,首先要明确爬取的数据.然后,思考从哪些地方可以获取这些数据.下面以一个实际案例来说明,怎么寻找一个好的爬虫策略.(代码仅供学习交流,切勿用作商业或其他有害行为) 1).方式一:直接爬取网站 ...

  4. Cannot enlarge string buffer containing XX bytes by XX more bytes

    在ELK的数据库报警系统中,发现有台机器报出了下面的错误: 2018-12-04 18:55:26.842 CST,"XXX","XXX",21106,&quo ...

  5. hdu 3068

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. bzoj 1503

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 12311  Solved: 4399[Submit][Stat ...

  7. VMware 安装Ubuntu16.04时显示不全的解决方法

    实际安装时发现进行到分区这个步骤时,看不到下面的按钮, 百度后得知有此遭遇的不在少数,是因为系统默认分辨率与电脑分辨率的差异导致的. 解决方法也很简单粗暴: 左手按住alt键右手鼠标往上拖动安装界面, ...

  8. Mongo副本集搭建

    解压mongodb-linux-x86_64-rhel70-3.2.0.tgz 将解压后的bin路径添加到系统环境变量,保证mongo.mongod等命令可用 创建副本集目录mongo/27017.2 ...

  9. mongodb 跟踪SQL语句及慢查询收集

    有个需求:跟踪mongodb的SQL语句及慢查询收集 第一步:通过mongodb自带函数可以查看在一段时间内DML语句的运行次数. 在bin目录下面运行  ./mongostat -port 端口号  ...

  10. 数据结构编程实验——chapter10-应用经典二叉树编程

    二叉树不仅结构简单.节省内存,更重要是是这种结构有利于对数据的二分处理.之前我们提过,在二叉树的基础上能够派生很多经典的数据结构,也是下面我们将进行讨论的知识点: (1)   提高数据查找效率的二叉排 ...