java写入excel文件poi
java写入excel文件
java写入excel文件poi,支持xlsx与xls,没有文件自动创建
package com.utils; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.exception.SimpleException; /**
* 从excel读取数据/往excel中写入 excel有表头,表头每列的内容对应实体类的属性
*
* @author nagsh
*
*/
public class ExcelManage { public static void main(String[] args) throws IOException {
String path = "E:/";
String fileName = "被保险人员清单(新增)04";
String fileType = "xlsx";
List<InsuraceExcelBean> list = new ArrayList<>();
for(int i=0; i<6; i++){
InsuraceExcelBean bean = new InsuraceExcelBean();
bean.setInsuraceUser("test"+i);
bean.setBankCardId("4444444444"+i+","+"55544444444444"+i+","+"999999999999999"+i);
bean.setIdCard("666666"+i);
bean.setBuyTime("2016-05-06");
bean.setInsEndTime("2016-05-07");
bean.setInsStartTime("2017-05-06");
bean.setMoney("20,000");
bean.setType("储蓄卡"); list.add(bean);
}
String title[] = {"被保险人姓名","身份证号","账户类型","银行卡号","保险金额(元)","购买时间","保单生效时间","保单失效时间"};
// createExcel("E:/被保险人员清单(新增).xlsx","sheet1",fileType,title); writer(path, fileName, fileType,list,title);
} @SuppressWarnings("resource")
public static void writer(String path, String fileName,String fileType,List<InsuraceExcelBean> list,String titleRow[]) throws IOException {
Workbook wb = null;
String excelPath = path+File.separator+fileName+"."+fileType;
File file = new File(excelPath);
Sheet sheet =null;
//创建工作文档对象
if (!file.exists()) {
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(); } else if(fileType.equals("xlsx")) { wb = new XSSFWorkbook();
} else {
throw new SimpleException("文件格式不正确");
}
//创建sheet对象
sheet = (Sheet) wb.createSheet("sheet1");
OutputStream outputStream = new FileOutputStream(excelPath);
wb.write(outputStream);
outputStream.flush();
outputStream.close(); } else {
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(); } else if(fileType.equals("xlsx")) {
wb = new XSSFWorkbook(); } else {
throw new SimpleException("文件格式不正确");
}
}
//创建sheet对象
if (sheet==null) {
sheet = (Sheet) wb.createSheet("sheet1");
} //添加表头
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
row.setHeight((short) 540);
cell.setCellValue("被保险人员清单"); //创建第一行 CellStyle style = wb.createCellStyle(); // 样式对象
// 设置单元格的背景颜色为淡蓝色
style.setFillForegroundColor(HSSFColor.PALE_BLUE.index); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(CellStyle.ALIGN_CENTER);// 水平
style.setWrapText(true);// 指定当单元格内容显示不下时自动换行 cell.setCellStyle(style); // 样式,居中 Font font = wb.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName("宋体");
font.setFontHeight((short) 280);
style.setFont(font);
// 单元格合并
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
sheet.autoSizeColumn(5200); row = sheet.createRow(1); //创建第二行
for(int i = 0;i < titleRow.length;i++){
cell = row.createCell(i);
cell.setCellValue(titleRow[i]);
cell.setCellStyle(style); // 样式,居中
sheet.setColumnWidth(i, 20 * 256);
}
row.setHeight((short) 540); //循环写入行数据
for (int i = 0; i < list.size(); i++) {
row = (Row) sheet.createRow(i+2);
row.setHeight((short) 500);
row.createCell(0).setCellValue(( list.get(i)).getInsuraceUser());
row.createCell(1).setCellValue(( list.get(i)).getIdCard());
row.createCell(2).setCellValue(( list.get(i)).getType());
row.createCell(3).setCellValue(( list.get(i)).getBankCardId());
row.createCell(4).setCellValue(( list.get(i)).getMoney());
row.createCell(5).setCellValue(( list.get(i)).getBuyTime());
row.createCell(6).setCellValue(( list.get(i)).getInsStartTime());
row.createCell(7).setCellValue(( list.get(i)).getInsEndTime());
} //创建文件流
OutputStream stream = new FileOutputStream(excelPath);
//写入数据
wb.write(stream);
//关闭文件流
stream.close();
} }
可以直接测试通过
poi处理,要jar的可以去下载也可以留言传送过去poi-3.12.jar,poi-ooxml-3.12.jar,poi-ooxml-schemas-3.12.jar
下载地址 http://i.cnblogs.com/Files.aspx (poi.zip包)
网盘下载jar包:
https://pan.baidu.com/s/18XXedGT0KToE4Tpihf4Quw 提取码:mers
偶遇晨光
2016-05-26
java写入excel文件poi的更多相关文章
- C++读写EXCEL文件OLE,java读写excel文件POI 对比
C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...
- java读写excel文件( POI解析Excel)
package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...
- 使用java写入excel文件
要操作excle文件,首先要下载jxl.jar文件,我用的版本是2.6.下载地址:http://www.andykhan.com/jexcelapi/download.html. Java Excel ...
- 从网络上获取图片,并写入excel文件
package com.weChat.utils; import com.manage.utils.DateUtil;import com.manage.utils.MD5Util;import or ...
- 一脸懵逼学习Java操作Excel之POI(Apache POI)
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1:下面简单的程序来创建一个空白Microsoft ...
- JXL包大解析;Java程序生成excel文件和解析excel文件内容
最近需求变化,需要把excel导入 我以前没有做过,所以我查了一些资料 和参考别人的代码 以下是多种方式: import java.io.File; import java.io.FileInputS ...
- Java读取Excel文件的几种方法
Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...
- 利用java读写Excel文件
一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...
- java读取excel文件的代码
如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...
随机推荐
- iOS、swift、React Native学习常用的社区、论坛
<!----iOS> <!----Swift>*IOS开发常用社区:http://code4app.com/ *IOS开发常用社区:http://www.cocoachina. ...
- java_SE(Day15)_集合1
一.集合类概述: 1.为什么出现集合类? 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类. 2.数组和集合类同是容器,有何不同? 数组虽然也可以存储对象 ...
- 学习笔记-Java编程思想
2016-01-03 Swith(整数选择因子):必须是int或char这样的整数值. Java中不包含goto语句,但是可以通过标识符实现类似的控制.
- LeetCode 【31. Next Permutation】
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 初识linux端c++程序开发
关于linux端程序开发,我以前一直不知道是做些什么,只是感觉听高端的.最近接触了一些,有了一些初步的认识. 首先,linux是一个操作系统,跟windows一样:接通电源.按下电脑开机,电脑就会自动 ...
- JSPatch常见问题解答
原文地址:https://github.com/bang590/JSPatch/wiki/JSPatch%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E8%A7%A3%E7 ...
- 用Windows PowerShell 控制管理 Microsoft Office 365
如果想要通过PowerShell控制管理Office365,首先要安装Microsoft Online Services Sign-In Assistant 7.0,链接如下 Microsoft On ...
- Http中 Post和 Get的区别
1.表面上的区别 1.GET在浏览器回退时,是无害的,而Post会再次提交请求 2.Get产生的Url地址会被Bookmark,而Post不会 3.Get请求会被浏览器主动Cache,而Post不会, ...
- oracle单机改变归档路径
oracle 归档日志文件路径设置 1. 查看LOG_ARCHIVE_DEST 与 ( LOG_ARCHIVE_DEST_n 或 DB_RECOVERY_FILE_DEST )参数情况注意( LO ...
- AI(一):概念与资讯
AI: Artificial Intelligence(人工智能),它是研究.开发用于模拟和扩展人的智能的理论.方法.技术及应用系统的一门新的技术科学,上个世纪50年代一次学术讨论会议上,下图中几位著 ...