java hssf 写 excle

编程技术  /  houtizong 发布于 3年前   57
在 poi-2.5.1.jar 下

package com.club.community.util;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Logger;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;import com.club.exception.BusinessAccessException;public class CreateExcelUtil {private static final Logger log = Logger.getLogger(CreateExcelUtil.class);private static final String SEPARATOR = File.separator;public static void crateExcel(String path, String fileName, String sheetName, List<String> titleList, List<String[]> contentList ) {//, List<String> titleList, List<String>log.info("生成excel开始,路径: " + path + SEPARATOR + fileName);HSSFWorkbook workbook = new HSSFWorkbook(); //创建新的Excel工作薄 HSSFSheet sheet;try {sheet = workbook.createSheet(new String(sheetName.getBytes(),"iso8859-1") );//在Excel工作薄中建工作表HSSFRow row = sheet.createRow((short) 0); //在索引0的位置建行(最顶端的行) HSSFCell cell;String title = "";for (int i = 0; i < titleList.size(); i++) {title = titleList.get(i);cell = row.createCell((short) i);//在索引0的位置建单元格cell.setEncoding(HSSFCell.ENCODING_UTF_16); //定义单元格为字符串类型cell.setCellValue(title);}String[] contents = new String[titleList.size()];for (int i = 0; i < contentList.size(); i++) {row = sheet.createRow((short) i + 1); //在索引1的位置创建行(最顶端的行) contents = contentList.get(i);for (int j = 0; j < contents.length; j++) {cell = row.createCell((short) j); //在索引0的位置创建单元格(左上端) cell.setEncoding(HSSFCell.ENCODING_UTF_16); //定义单元格为字符串类型  cell.setCellValue(contents[j]); //在单元格输入一些内容 }}} catch (UnsupportedEncodingException e) {log.error(e);throw new BusinessAccessException("生成excel失败");}String fullPathName = path + SEPARATOR + fileName;File f = new File(fullPathName);if(!f.exists()) {f.getParentFile().mkdirs();}FileOutputStream fOut;//新建输出文件流 try {fOut = new FileOutputStream(fullPathName);workbook.write(fOut); //把相应的Excel工作薄存盘 fOut.flush();fOut.close(); //操作结束,关闭文件 log.info("生成excel结束,路径: " + path + SEPARATOR + fileName);System.out.println("excel文件已经生成,存放在" + fullPathName);} catch (FileNotFoundException e) {log.error(e);log.info("生成excel结束,路径: " + path + SEPARATOR + fileName);throw new BusinessAccessException("生成excel失败");} catch (IOException e) {log.error(e);log.info("生成excel结束,路径: " + path + SEPARATOR + fileName);throw new BusinessAccessException("生成excel失败");}}public static void main(String[] args) {List<String> titleList = new ArrayList<String>();titleList.add("作者");titleList.add("编辑");List<String[]> contentList = new ArrayList<String[]>();String[] a1 = {"张三", "李四"};String[] a2 = {"王五", "徐六"};String[] a3 = {"2222", "3333"};String[] a4 = {"4444", "5555"};contentList.add(a1);contentList.add(a2);contentList.add(a3);contentList.add(a4);crateExcel("c:\\abcdefg", "aa.xls", "bb", titleList, contentList);}}

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客