在 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);
}
}
- poi-2.5.1.jar (783.4 KB)
- 下载次数: 0