public class excel2POI {
@SuppressWarnings("unchecked")
public void excel(String outputFile, List dataList)throws FileNotFoundException, IOException {
String is = "test.xls";
System.out.println(is);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(is)); // 用模板文件构造poi
HSSFWorkbook templatewb = new HSSFWorkbook(fs); // 创建模板工作表
HSSFSheet templateSheet = templatewb.getSheet("spotInfoExcel"); // 直接取模板第一个sheet对象
int columns = templateSheet.getRow((short) 0).getPhysicalNumberOfCells(); // 取得Excel文件的总列数
HSSFCellStyle styleArray[] = new HSSFCellStyle[columns]; // 创建样式数组
// 一次性创建所有列的样式放在数组里
for (int s = 0; s < columns; s++) {
// 得到数组实例
styleArray[s] = templatewb.createCellStyle();
}
HSSFRow row = null;
Map map = null;
HSSFCell cell = null;
int num = dataList.size();
for (int j = 0; j < num; j++) {
row = templateSheet.createRow(j + 1);
map = (Map) dataList.get(j);
for (int k = 0; k < map.keySet().size(); k++) {
cell = row.createCell(k);
if (null != map.get(k)) {
HSSFRichTextString tsetString = new HSSFRichTextString(map
.get(k).toString());
cell.setCellValue(tsetString.toString());// 列中放入值
}
}
map.clear();
map = null;
}
// 设置输入流
FileOutputStream fOut = new FileOutputStream(outputFile);
templatewb.write(fOut); // 将模板的内容写到输出文件上
fOut.flush();
// 操作结束,关闭文件
fOut.close();
}
public static void main(String[] args) {
List list = new ArrayList();
Map map = new HashMap();
map.put(0, "a");
map.put(1, "b");
map.put(2, "c");
Map map1 = new HashMap();
map1.put(0, "aa");
map1.put(1, "bb");
map1.put(2, "cc");
list.add(map);
list.add(map1);
try {
excel2POI ss = new excel2POI();
ss.excel("d:/test1.xls", list);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
分享到:
相关推荐
利用POI将数据导出到给定的EXCEL模板,并根据要求合并单元格。
commons-collections4-4.1.wso2v1.jar,commons-math3-3.0.jar;两个jar包下载,用于poi模板导出excel用
poi基于模板导出excel,poi基于模板导出excelpoi,基于模板导出excel
NULL 博文链接:https://jjxliu306.iteye.com/blog/2383610
POI使用excel模板循环输出行到文件并导出的一个小例子
使用Apache POI的3.16版实现,Excel模板填充数据导出Excel报表
poi导入、导出,支持百万级数据模板导出、合并excel。项目为spring-boot-2上开发。resource里面有模板,在junit测试类中修改为本地存在路径即可导出文件,在junit测试类中修改for循环的i可以模拟百万级数据导出。注意...
poi 基于excel模板导出 功能强大 代码清晰
POI模板导出并下载、并提供下载Utils
springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式...
poi excel通过模板导出excel表格 且带有公式 导出的excel表格里面的公式均可计算 有简单的公式也有难的公司 在此例子里面都可以跟着excel一起到处并可计算
用poi根据模板导出excel的帮助类,支持xls和xlsx两种格式,支持上万数据导出不会内存移除,代码有注释。
此代码包括poi导出excel的所有jar,自定义Excel模板后,将数据导入其中
实现了POI根据模板导出word,实现文字、表格、图表的替换生成。支持生成多种结构的表格、图表,关键代码都添加了注释,简单易懂。 其中表格和图表都写了两种实现方式,既可以只传简单的参数生成多个同结构的表格和...
java基于poi使用excel模板导出
Jxl与Poi模板导出Excel文件,超级简单,内有源码,可以看我的博客 https://blog.csdn.net/xll_csdn/article/details/106862121 收费不高,赚点积分,互利互惠,哈哈!
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
POI基于java导入导出Excel表格模板
springboot + poi导出指定格式Excel模板。 springboot+poi导出指定格式Excel模板,完整项目,导出即用。 poi导出Excel
用模板导入导出