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的更多相关文章

  1. C++读写EXCEL文件OLE,java读写excel文件POI 对比

    C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...

  2. java读写excel文件( POI解析Excel)

    package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...

  3. 使用java写入excel文件

    要操作excle文件,首先要下载jxl.jar文件,我用的版本是2.6.下载地址:http://www.andykhan.com/jexcelapi/download.html. Java Excel ...

  4. 从网络上获取图片,并写入excel文件

    package com.weChat.utils; import com.manage.utils.DateUtil;import com.manage.utils.MD5Util;import or ...

  5. 一脸懵逼学习Java操作Excel之POI(Apache POI)

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1:下面简单的程序来创建一个空白Microsoft ...

  6. JXL包大解析;Java程序生成excel文件和解析excel文件内容

    最近需求变化,需要把excel导入 我以前没有做过,所以我查了一些资料 和参考别人的代码 以下是多种方式: import java.io.File; import java.io.FileInputS ...

  7. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  8. 利用java读写Excel文件

    一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...

  9. java读取excel文件的代码

    如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...

随机推荐

  1. Xcode开发openCV for iOS 时#include <list> not found

    分析 在做混合编译之前一定要把编译器的Compile Sources As选项改为Objective C++. 默认的选项是According to file type,用这个的话,你后面每个不在交叉 ...

  2. 浅析c#中登录窗体和欢迎窗体关闭的问题

    第一次在cnbogs发文章,这次来个很基础的,主要给小白看. 在c#的winform编程中,我们经常会做登录窗体或欢迎窗体,并把他们作为启动窗体. 但是,我们有可能会遇到一些问题. 请看下面的代码: ...

  3. kbengine0.4.20源代码分析(一)

    基于kbengine 0.4.2 MMOPG服务端是一种高品质的工程项目,品读开源的kbe是一种乐趣.本文档我带童鞋们一起领略一下.囿于我知识面和经验方面所限,文中所述之处难免有错误存在,还请读童鞋们 ...

  4. Qt开发中的实用笔记一--xml,Qpainter,Delegate:

    因为开发环境不能联网,开发中用到有用的知识就记在word稳定中,不知不觉就记载了几十页,为避免笔记丢失,现在就一点点忘博客上搬,方便日后回顾! ---------------------------- ...

  5. RestEasy 3.x 系列之三:jsonp

    跨域请求解决方法(JSONP, CORS)提到解决跨域可以使用jsonp,RestEasy自带jsonp的拦截器 一.RestEasy的文档如下: If you're using Jackson, R ...

  6. C# 指定Webbrowser控件所用IE内核版本

    如果电脑上安装了IE8或者之后版本的IE浏览器,Webbrowser控件会使用IE7兼容模式来显示网页内容.解决方法是在注册表中为你的进程指定引用IE的版本号. 比如我的程序叫做a.exe,以64位机 ...

  7. C#模拟键盘事件

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("USER3 ...

  8. DBA-mysql-init-password-5.7

    1.Mysql5.7 Password; 查找临时密码:grep "A temporary password"  /var/log/mysqld.log 修改临时密码:alter ...

  9. [2015.08.13]万峰快递单打印软件 v2.2

    <万峰快递单打印软件>是一款简单易用,且支持所有快递单的打印软件.1.支持文字打印位置的坐标精确定位和微调的打印工具.2.操作灵活,只需录入收件人和发件人信息即可打印:3.快递单打印尺寸和 ...

  10. 解决duplicate symbols for architecture x86_64错误

    duplicate symbols for architecture x86_64 两个不第三方SDK之间的文件里面内容重复了,类似 file.h+file.m 和 CHfile.h+CHfile.m ...