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. STM32 ------ HardFault_Hander 中断函数

    1.数组越界(对数组赋值) 正常情况,数组越界会进入 HardFault_Hander 中断函数的无线循环. 避免数组越界的一个方法是:每次使用数组前,检查要放入数据的数据长度是否大于数组长度,大于则 ...

  2. ElasticStack系列之八 & _source 字段

    有很多人会有这样的一个疑问: _source字段存储的是索引的原始内容,那 store 属性的设置是为何呢?elasticsearch 为什么要把 store 的默认取值设置为 no?设置为 yes ...

  3. day11 细节记忆

    单一职责:一个方法只做一件事. 值传递——java只有值传递. gender(性别) male(男)female(女) 自动生成的set.get方法中,布尔类型的get方法需要手工改为get(默认是i ...

  4. IntelliJ IDEA连接cvs超时Error refreshing view: Timeout while trying to connect to host

    在使用IntelliJ IDEA连接cvs的时候,有时会报超时错误: Error refreshing view: Timeout while trying to connect to host: 1 ...

  5. Eclipse安卓插件安装

    首先说明下载的ADT专门真安卓开发的Eclipse下载下来后就集成了可以直接使用了 但是使用j2EE版本的Eclipse就需要安装插件支持安卓开发了 首先下载ADT Eclipse安卓插件 下载完成后 ...

  6. windows git gui右键sublime打开当前文件编辑

    git安装目录\Git\libexec\git-core\git-gui.tcl的 proc create_common_diff_popup 下追加: $ctxm add command \ -la ...

  7. 【Linux】VirtualBox安装ubuntu排错LowGraphic

    在Oracle的VirtualBox虚拟机上,安装Ubuntu后,提示了如下图这样的 错误 The system is running in low-graphics mode 在网上搜,有多种解答方 ...

  8. Mother's Mil 母亲的牛奶

    Description 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装 ...

  9. MongoDB警告信息

    更多内容推荐微信公众号,欢迎关注: MongoDB警告信息: 1. WARNING: Using the XFS filesystem is strongly recommended with the ...

  10. 2016.5.21——atoi()函数的测试

    对函数atoi()函数的测试: atoi()函数将字符串型转换为整型 代码: #include "stdafx.h" #include "iostream" # ...