java操作poi生成excel文件(.xlsx)
来源:互联网 发布:掌握了c语言之后学什么 编辑:程序博客网 时间:2024/06/05 04:31
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XlsDto2ExcelEx {
/**
*
* @param xls
* XlsDto实体类的一个对象
* @throws Exception
* 在导入Excel的过程中抛出异常
*/
public static void xlsDto2Excel(List<User> xls) throws Exception {
// 获取总列数
int CountColumnNum = 5;
XSSFWorkbook hwb = new XSSFWorkbook();
XSSFCellStyle style = hwb.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
User user = null;
XSSFSheet sheet = hwb.createSheet();
sheet.setColumnWidth(0,256*2*7);//一个汉字两个字符
sheet.setColumnWidth(1,256*2*11);
sheet.setColumnWidth(2,256*2*6);
sheet.setColumnWidth(3,256*2*5);
sheet.setColumnWidth(4,256*2*11);
// sheet 对应一个工作页
// XSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
XSSFRow firstrow = sheet.createRow(0);
XSSFCell[] firstcell = new XSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "手机号";
names[1] = "身份证号";
names[2] = "资信平台";
names[3] = "击中规则";
names[4] = "时间";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new XSSFRichTextString(names[j]));;
firstcell[j].setCellStyle(style);
}
for (int i = 0; i < xls.size(); i++) {
// 创建一行
XSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
user = xls.get(i);
XSSFCell xh0 = row.createCell(0);
xh0.setCellValue(user.getId());
xh0.setCellStyle(style);
XSSFCell xh1 = row.createCell(1);
xh1.setCellValue(user.getNo());
xh1.setCellStyle(style);
XSSFCell xh2 = row.createCell(2);
xh2.setCellValue(user.getPlatform());
XSSFCell xh3 = row.createCell(3);
xh3.setCellValue(user.getRule());
XSSFCell xh4 = row.createCell(4);
xh4.setCellValue(user.getTime());
xh4.setCellStyle(style);
}
// 创建文件输出流,准备输出电子表格
//当HSSF类型的excel时,新建excel需要放在工程下POI2Excel的文件夹中。而为XSSF时,默认放在工程目录下。
//HSSF版本的excel(2003)最大可存入65536条记录,而XSSF版本的excel则可存放1048576条记录
OutputStream out = new FileOutputStream("user.xlsx");
hwb.write(out);
out.close();
System.out.println("数据库导出成功");
}
public static void main(String[] args){
List<User> list = new ArrayList<User>();
for(int i = 0; i< 1000; i++){
list.add(new User("13512341204","441900196706299382", "同盾","D101", "2017-01-21 18:46:01"));
list.add(new User("15811111326","340303197703055389", "同盾","D101", "2017-01-21 18:47:00"));
list.add(new User("13512340067","653224197705141266", "同盾","D101", "2017-01-21 18:48:00"));
list.add(new User("15811111335","320900196710294535", "同盾","D101", "2017-01-21 18:49:00"));
list.add(new User("17700000424","510502199512057758", "同盾","D101", "2017-01-21 18:50:00"));
list.add(new User("14761508264","150701199107028299", "同盾","D101", "2017-01-21 18:51:00"));
list.add(new User("14760656721","632821198703038744", "聚立信","D101", "2017-01-21 18:52:00"));
list.add(new User("15845611122","421182198409244752", "同盾","D101", "2017-01-21 18:53:00"));
list.add(new User("13334557298","420704197711171695", "同盾","D101", "2017-01-21 18:55:00"));
}
try {
xlsDto2Excel(list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class User{
public User(String id,String no, String platform,String rule, String time){
this.id = id;
this.no = no;
this.platform = platform;
this.rule = rule;
this.time = time;
}
private String id;
private String no;
private String platform;
private String rule;
private String time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getRule() {
return rule;
}
public void setRule(String rule) {
this.rule = rule;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String toString(){
return "id:" + this.id + ",no" + this.no + ",platform:" + this.platform + ",rule:" + this.rule + ", time:"+ this.time;
}
}
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XlsDto2ExcelEx {
/**
*
* @param xls
* XlsDto实体类的一个对象
* @throws Exception
* 在导入Excel的过程中抛出异常
*/
public static void xlsDto2Excel(List<User> xls) throws Exception {
// 获取总列数
int CountColumnNum = 5;
XSSFWorkbook hwb = new XSSFWorkbook();
XSSFCellStyle style = hwb.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
User user = null;
XSSFSheet sheet = hwb.createSheet();
sheet.setColumnWidth(0,256*2*7);//一个汉字两个字符
sheet.setColumnWidth(1,256*2*11);
sheet.setColumnWidth(2,256*2*6);
sheet.setColumnWidth(3,256*2*5);
sheet.setColumnWidth(4,256*2*11);
// sheet 对应一个工作页
// XSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
XSSFRow firstrow = sheet.createRow(0);
XSSFCell[] firstcell = new XSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "手机号";
names[1] = "身份证号";
names[2] = "资信平台";
names[3] = "击中规则";
names[4] = "时间";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new XSSFRichTextString(names[j]));;
firstcell[j].setCellStyle(style);
}
for (int i = 0; i < xls.size(); i++) {
// 创建一行
XSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
user = xls.get(i);
XSSFCell xh0 = row.createCell(0);
xh0.setCellValue(user.getId());
xh0.setCellStyle(style);
XSSFCell xh1 = row.createCell(1);
xh1.setCellValue(user.getNo());
xh1.setCellStyle(style);
XSSFCell xh2 = row.createCell(2);
xh2.setCellValue(user.getPlatform());
XSSFCell xh3 = row.createCell(3);
xh3.setCellValue(user.getRule());
XSSFCell xh4 = row.createCell(4);
xh4.setCellValue(user.getTime());
xh4.setCellStyle(style);
}
// 创建文件输出流,准备输出电子表格
//当HSSF类型的excel时,新建excel需要放在工程下POI2Excel的文件夹中。而为XSSF时,默认放在工程目录下。
//HSSF版本的excel(2003)最大可存入65536条记录,而XSSF版本的excel则可存放1048576条记录
OutputStream out = new FileOutputStream("user.xlsx");
hwb.write(out);
out.close();
System.out.println("数据库导出成功");
}
public static void main(String[] args){
List<User> list = new ArrayList<User>();
for(int i = 0; i< 1000; i++){
list.add(new User("13512341204","441900196706299382", "同盾","D101", "2017-01-21 18:46:01"));
list.add(new User("15811111326","340303197703055389", "同盾","D101", "2017-01-21 18:47:00"));
list.add(new User("13512340067","653224197705141266", "同盾","D101", "2017-01-21 18:48:00"));
list.add(new User("15811111335","320900196710294535", "同盾","D101", "2017-01-21 18:49:00"));
list.add(new User("17700000424","510502199512057758", "同盾","D101", "2017-01-21 18:50:00"));
list.add(new User("14761508264","150701199107028299", "同盾","D101", "2017-01-21 18:51:00"));
list.add(new User("14760656721","632821198703038744", "聚立信","D101", "2017-01-21 18:52:00"));
list.add(new User("15845611122","421182198409244752", "同盾","D101", "2017-01-21 18:53:00"));
list.add(new User("13334557298","420704197711171695", "同盾","D101", "2017-01-21 18:55:00"));
}
try {
xlsDto2Excel(list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class User{
public User(String id,String no, String platform,String rule, String time){
this.id = id;
this.no = no;
this.platform = platform;
this.rule = rule;
this.time = time;
}
private String id;
private String no;
private String platform;
private String rule;
private String time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getRule() {
return rule;
}
public void setRule(String rule) {
this.rule = rule;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String toString(){
return "id:" + this.id + ",no" + this.no + ",platform:" + this.platform + ",rule:" + this.rule + ", time:"+ this.time;
}
}
0 0
- java操作poi生成excel文件(.xlsx)
- POI生成(Excel 2007).xlsx文件时报“java.lang.NoSuchMethodError”错误的解决
- POI-处理大Excel文件(xlsx)
- JAVA 创建excel(xlsx)POI技术
- Java利用POI生成Excel(.xls和.xlsx)
- java:poi读取excel文件,分xls和xlsx
- java使用POI读取excel文件,兼容xls和xlsx
- Java POI生成一个指定模板的.xlsx文件
- Java POI 导出Oracle数据表写入Excel文件(Excel2007 xlsx)
- Java Poi操作Excel文件
- java使用poi读取excel数据(xlsx)
- Java 实现导出excel表 POI/Java实现不同excel格式(*.xls、*.xlsx)文件的读取
- POI读取EXCEL下xlsx,xls文件
- Java对poi操作生成Excel表
- Java生成excel文件-本地生成excel文件-poi
- Java POI生成Excel表文件
- Java使用poi生成Excel文件
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI生成Excel文件(一)
- android studio1.x升级到android studio2.2遇到的坑
- ES权威指南_01_get start_05 Searching—The Basic Tools
- Xcode8 检测内存泄露
- spring事务配置总结
- 小白笔记--------------------------安装hadoop集群简单总结
- java操作poi生成excel文件(.xlsx)
- 微信小程序--swiper图片显示不完整
- 睡眠办法a
- Android内存优化基础
- 堆区和栈区的区别
- js几点小总结
- ES权威指南_01_get start_06 Mapping and Analysis
- Activity透明/半透明效果的设置transparent(两种实现方法)
- 设计模式—建造者模式及实例(BuilderPattern)