java批量生成excel文件
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文件的更多相关文章
- java批量生成excel代码分享
package com.test.util; /** * @author ocq * */ import java.io.FileOutputStream; import java.io.IOExce ...
- XLSTransformer生成excel文件简单演示样例
项目结构图: 项目中所用到的jar,能够到http://www.findjar.com/index.x下载 ExcelUtil类源代码: package util; import java.io.IO ...
- XLSTransformer生成excel文件
jxls的使用方法: 1)声明一个XLSTransformer对象,生成方式就是使用new操作符 XLSTransformer transformer = new XL ...
- 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件
今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...
- springMVC(4)---生成excel文件并导出
springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...
- java上传excel文件及解析
java上传excel文件及解析 CreateTime--2018年3月5日16:25:14 Author:Marydon 一.准备工作 1.1 文件上传插件:swfupload: 1.2 文件上 ...
- java代码将excel文件中的内容列表转换成JS文件输出
思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...
- FluentData-新型轻量级ORM 利用T4模板 批量生成多文件 实体和业务逻辑 代码
FluentData,它是一个轻量级框架,关注性能和易用性. 下载地址:FlunenData.Model 利用T4模板,[MultipleOutputHelper.ttinclude]批量生成多文件 ...
- 如何生成excel文件作为图像识别结果
如何生成excel文件作为图像识别结果 在进行大规模图像处理的时候,如果能够以表格的形式生成结果文件,将非常的直观.这个时候,选择excel作为结果输出文件,将是合适的. 查询相关资料,有很多关于ex ...
随机推荐
- 1003 Emergency(25 分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- [spark]-Spark2.x集群搭建与参数详解
在前面的Spark发展历程和基本概念中介绍了Spark的一些基本概念,熟悉了这些基本概念对于集群的搭建是很有必要的.我们可以了解到每个参数配置的作用是什么.这里将详细介绍Spark集群搭建以及xml参 ...
- 数据分析与展示---Pandas库入门
简介 一:Pandas库的介绍 二:Pandas库的Series类型 (一)索引 (1)自动索引 (2)自定义索引 (二)Series类型创建 (1)列表创建 (2)标量值创建 (3)字典类型创建(将 ...
- css3-自定义字体
参考链接http://www.w3cplus.com/content/css3-font-face 出处W3CPLUS css3-自定义字体 @font-face @font-face是CSS3中 ...
- redis内存模型
前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可或缺的一部分. 我们使用Redis时,会接触Redis的5种对象类型(字符串 ...
- 【整理】HTML5游戏开发学习笔记(1)- 骰子游戏
<HTML5游戏开发>,该书出版于2011年,似乎有些老,可对于我这样没有开发过游戏的人来说,却比较有吸引力,选择自己感兴趣的方向来学习html5,css3,相信会事半功倍.不过值得注意的 ...
- 2014年最佳的10款 PHP 开发框架
PHP去年发生了翻天覆地的变化.似乎每个人都有一个想法一个好的框架应该是什么样子,但话又说回来,没有多少面积制品类型的框架或框架的最终实际使用在不同的生产项目. 你知道哪个框架选择为您的生产计划吗?你 ...
- WPF控件收集
1.Extended WPF Toolkit 2.Fluent Ribbon Control Suite 3.WPF Ribbon Control 4.Telerik RadControls for ...
- [csp-201509-3]模板生成系统
#include<bits/stdc++.h> using namespace std; ; string a[N],b[N],c[N]; int main() { //freopen(& ...
- 对某道ctf的一点点记录
题目:http://ctf5.shiyanbar.com/web/pcat/index.php 是一道注入的题,主要利用了offset 和 group by with rollup的知识 1.offs ...