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. 计算机网络自学之路-----IP协议(3)

    前面一期说到了IP层的IP协议跟ARP协议,因为IPV4协议自身有些不足的地方,为了弥补这些不足,又引入了一些别的协议.觉得这种弥补方式治标不治本~~ 1)ICMP网络控制报文协议 2)CIDR无类域 ...

  2. CE 定时器

    //头文件: afx_msg void OnTimer(UINT_PTR nIDEvent); //声明定时器响应函数 //代码文件: #define TIMER 1 #define TIMEROUT ...

  3. Zookeeper 的学习与运用

    引子 云计算越来越流行的今天,单一机器处理能力已经不能满足我们的需求,不得不采用大量的服务集群.服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,这些信息如何推送到各个节点? ...

  4. 【IOS学习】1.IOS框架

    1.框架概述 iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa ...

  5. python学习之python开发环境搭建

    Python简介 Python是一种面向对象.解释型计算机程序设计语言.Python语法简洁而清晰,具有丰富和强大的类库等等众多的特性,这是来自百度百科的介绍,在百度百科还能看到它的更详细的介绍信息, ...

  6. (转)网上总结的 NIPS 201 参会感受

    1. http://www.machinedlearnings.com/2016/12/nips-2016-reflections.html 2. http://blog.arpitmohan.com ...

  7. phpstorm 配置 babel 支持EcmaScript6

    1.安装nodejs 2.npm install --save-dev babel-cli 3.npm install babel-preset-es2015 --save-dev 4.phpstor ...

  8. awk 的使用

    awk [-F field-separator] 'commands' input-file(s) 其中,commands 是真正awk命令,[-F域分隔符]是可选的.input-file(s) 是待 ...

  9. Window memcache 使用

    一.memcache配置 1. 下载memcache 32位系统 1.2.5版本:http://static.runoob.com/download/memcached-1.2.5-win32-bin ...

  10. coffeeScript中类的多态[学习篇]

    类的一大应用就是多态.多态是一个面向对象编程的高级术语----“一个东西可编程很多不同的东西,但不是任何东西”.[引自coffeescript深入浅出] class Shape constructor ...