POI使用案例02---向excel中写入数据
来源:互联网 发布:国密 java 编辑:程序博客网 时间:2024/06/07 22:15
1、一个简单的输出
@Test public void testWrite() throws IOException { Workbook workbook2003 = new HSSFWorkbook(); Workbook workbook2007 = new XSSFWorkbook(); Workbook workbook2016 = new SXSSFWorkbook(); //创建一个sheet Sheet sheet = workbook2003.createSheet("workbook2003"); //创建一行 Row row = sheet.createRow(0); //设置行高 row.setHeightInPoints(30); //创建一个单元格 Cell cell = row.createCell(0); cell.setCellValue("长度");//设置值 cell = row.createCell(1); cell.setCellValue("城市区号"); FileOutputStream fos = new FileOutputStream(new File("wb2.xls")); //写到输出流 workbook2003.write(fos); fos.close(); }
二、poi功能集成。
为了方便, 我们希望从一个模板中获取输出的样式。
2.1、模板: 中间为数据样式, 以test的参照, 从此行开始插入数据
2.2、创建模板类, 设置为单例模式,
2.2.1、读取模板,
public ExcelTemplate readTemplateByPath(String path) { try { workbook = WorkbookFactory.create(new File(path)); initTemplate(); } catch (EncryptedDocumentException e) { e.printStackTrace(); throw new RuntimeException("文档加密了..."); } catch (InvalidFormatException e) { e.printStackTrace(); throw new RuntimeException("文档的格式有错!"); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("io exception..."); } return et; }
2.2.2、initTemplate():初始化模板参数, 遍历模板default.xls文件, 找到test所在的cell, 初始化开始写数据行和列
2.2.3、接下来就是按照表头格式一个单元格的往下写,注意写到了行尾, 换下一行
注意: 创建新的单元格时候, 使用createRow, 不要搞成了getCell, 我第一次就犯这个错误, 输出的excel文件总是大小为0, 打开时总是包格式有问题。
2.2.4、测试:
@Test public void testTem() { ExcelTemplate et = ExcelTemplate.newInstance().readTemplateByPath("G:\\JavaWeb\\POIUtils\\src\\main\\resources\\default.xls");; et.createCell("chb"); et.createCell("111"); et.createCell("开发人员"); et.createCell("1813"); et.createCell("1813@19.com"); et.createCell("poi写出"); //。。。creatCell... }
2.2.5、输出效果: 除了没有处理尾部, 其他输出正常
尾部应该在插入数据时下移。
2.2.6、完整代码
package com.chb.poi.util;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.EncryptedDocumentException;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellType;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.usermodel.WorkbookFactory;/** * * @author 12285 */public class ExcelTemplate { private final static String DATA_LINE = "test"; private Workbook workbook = null; private Sheet sheet = null; private Row curRow = null; private static ExcelTemplate et = new ExcelTemplate(); /**开始写数据的列序号 */ private int initColIndex = 0; /** 开始写数据的行序号*/ private int initRowIndex = 1; private int curColIndex; private int curRowIndex; /** * 模板的最后一列 */ private int lastColIndex; private ExcelTemplate() {} /** * * @return */ public static ExcelTemplate newInstance() { return et; } /** * 读取相应的模板文档 * @param path 模板文档 */ public ExcelTemplate readTemplateByclasspath(String path) { try { workbook = WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(path)); initTemplate(); } catch (EncryptedDocumentException e) { throw new RuntimeException("文档加密了..."); } catch (InvalidFormatException e) { throw new RuntimeException("文档的格式有错!"); } catch (IOException e) { throw new RuntimeException("io exception..."); } return et; } public ExcelTemplate readTemplateByPath(String path) { try { workbook = WorkbookFactory.create(new File(path)); initTemplate(); } catch (EncryptedDocumentException e) { e.printStackTrace(); throw new RuntimeException("文档加密了..."); } catch (InvalidFormatException e) { e.printStackTrace(); throw new RuntimeException("文档的格式有错!"); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("io exception..."); } return et; } /** * 创建cell, 设置值 * @param value */ public void createCell(String value) { Cell cell = curRow.createCell(curColIndex); cell.setCellValue(value); curColIndex++; //写完最后一列, 换一个新行。 if (lastColIndex== curColIndex) { createNewRow(); } } /** * 当一行数据写完, 将开始从下一行开始写 */ public void createNewRow() { curRowIndex++; curRow = sheet.createRow(curRowIndex); //一行写完, 从开始的列开始些 curColIndex = initColIndex; } public void writeToFile(File filepath) { FileOutputStream fos = null; try { fos = new FileOutputStream(filepath); workbook.write(fos); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace();} } } } /** * 初始化 行, 列 * 有了模板文档,我们需要知道从哪儿开始是写数据 */ private void initTemplate() { sheet = workbook.getSheetAt(0);//获取第一个sheet lastColIndex = sheet.getRow(initRowIndex-1).getLastCellNum();//获取列头的列数 initConfigData(); //初始化第一行数据 curRow = sheet.createRow(initRowIndex); } /** * 初始化行列 */ private void initConfigData() { boolean findedData = false; //遍历模板, 找到写数据的开始的位置 for (Row row:sheet) { if(findedData) break; for (Cell cell : row) { if (cell.getCellTypeEnum() == CellType.STRING) { String test = cell.getStringCellValue(); if (DATA_LINE.equals(test)) {//找到开始写数据的位置 initRowIndex = cell.getRowIndex(); initColIndex = cell.getColumnIndex(); curRowIndex = initRowIndex; curColIndex = initColIndex; break; } } } } System.out.println("initRowIndex:" + initRowIndex + "," + "initColIndex:" + initColIndex); }}
2.3、尾部的两行往下移动。
2.3.1 下移使用shiftRows,
void org.apache.poi.ss.usermodel.Sheet.shiftRows( int startRow, //从哪行开始 int endRow, //到那行下移 int n, boolean //下移n行 copyRowHeight, boolean resetOriginalRowHeight)
2.3.2、因为是尾部下移,所以需要知道最后一行
//最后一行 private int lastRowIndex;
2.3.2、什么时候下移? 在写新的一行的时候, 但是注意: 在写入第一行完了, 没有写新的数据, 尾部不用下移,因为第一行是在test所在行写入。
写完最后一列,是否创建新的行, 根据是否有新的数据, 所以判断发在createCell()开始。
2.3.3、完整代码
package com.chb.poi.util;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.EncryptedDocumentException;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellType;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.usermodel.WorkbookFactory;/** * * @author 12285 */public class ExcelTemplate { private final static String DATA_LINE = "test"; private Workbook workbook = null; private Sheet sheet = null; private Row curRow = null; private static ExcelTemplate et = new ExcelTemplate(); /**开始写数据的列序号 */ private int initColIndex = 0; /** 开始写数据的行序号*/ private int initRowIndex = 1; private int curColIndex; private int curRowIndex; /** * 模板的最后一列 */ private int lastColIndex; //最后一行 private int lastRowIndex; private ExcelTemplate() {} /** * * @return */ public static ExcelTemplate newInstance() { return et; } /** * 读取相应的模板文档 * @param path 模板文档 */ public ExcelTemplate readTemplateByclasspath(String path) { try { workbook = WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(path)); initTemplate(); } catch (EncryptedDocumentException e) { throw new RuntimeException("文档加密了..."); } catch (InvalidFormatException e) { throw new RuntimeException("文档的格式有错!"); } catch (IOException e) { throw new RuntimeException("io exception..."); } return et; } public ExcelTemplate readTemplateByPath(String path) { try { workbook = WorkbookFactory.create(new File(path)); initTemplate(); } catch (EncryptedDocumentException e) { e.printStackTrace(); throw new RuntimeException("文档加密了..."); } catch (InvalidFormatException e) { e.printStackTrace(); throw new RuntimeException("文档的格式有错!"); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("io exception..."); } return et; } /** * 创建cell, 设置值 * @param value */ public void createCell(String value) { //写完最后一列, 换一个新行。 if (lastColIndex== curColIndex) { createNewRow(); } Cell cell = curRow.createCell(curColIndex); cell.setCellValue(value); curColIndex++; } /** * 当一行数据写完, 将开始从下一行开始写 */ public void createNewRow() { curRowIndex++; sheet.shiftRows(curRowIndex, lastRowIndex++, 1, true, true); curRow = sheet.createRow(curRowIndex); //一行写完, 从开始的列开始些 curColIndex = initColIndex; } public void writeToFile(File filepath) { FileOutputStream fos = null; try { fos = new FileOutputStream(filepath); workbook.write(fos); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace();} } } } /** * 初始化 行, 列 * 有了模板文档,我们需要知道从哪儿开始是写数据 */ private void initTemplate() { sheet = workbook.getSheetAt(0);//获取第一个sheet initConfigData(); //初始化第一行数据, 不用下移尾部 curRow = sheet.createRow(initRowIndex); } /** * 初始化行列 */ private void initConfigData() { boolean findedData = false; //遍历模板, 找到写数据的开始的位置 for (Row row:sheet) { if(findedData) break; for (Cell cell : row) { if (cell.getCellTypeEnum() == CellType.STRING) { String test = cell.getStringCellValue(); if (DATA_LINE.equals(test)) {//找到开始写数据的位置 initRowIndex = cell.getRowIndex(); initColIndex = cell.getColumnIndex(); curRowIndex = initRowIndex; curColIndex = initColIndex; break; } } } } lastColIndex = sheet.getRow(initRowIndex-1).getLastCellNum();//获取表头的列数 lastRowIndex = sheet.getLastRowNum();//最后一行 System.out.println("initRowIndex:" + initRowIndex + "," + "initColIndex:" + initColIndex + "lastColIndex:" + lastColIndex); }}
阅读全文
0 0
- POI使用案例02---向excel中写入数据
- 使用POI 向Excel中追加数据
- java向EXCEL文件中写入数据使用jxljar包
- 使用python向excel中写入数据(中英文混合)
- Python中向excel中写入数据
- 使用Apache POI创建Excel,并从数据库中读取数据写入到Excel文件中
- 用POI向Excel中写入多张图片
- Python向excel中写入数据
- 使用C#向Excel文档写入数据
- 使用C#向Excel文档写入数据
- poi读取数据写入excel
- java poi技术操作excel之写Excel(向Excel中写入值)
- POI向EXCEL写入图片(转)
- 通过POI组件把数据写入到Excel文件中
- VS2010中使用C#实现向EXCEL文档中写入数据
- 读取txt数据,向excel中写入数据
- POI使用案例01---读取excel的数据
- 基于Apache POI 向xlsx写入数据
- 委托的学习以及使用
- 笔记:关于hibernate在dao层要导入的包
- 史上最简单的react脚手架搭建
- linux 环境下JDK的安装+环境变量配置
- 【C#与.NET程序设计】(4)- C#类及OOP
- POI使用案例02---向excel中写入数据
- OPENCV3+VS2015配置教程
- 蒙特卡洛算法简单理解与demo
- 我的第一个实验
- 栈与堆栈的区别
- EmailUtils--QQ
- 如何解决高并发下缓存被击穿的问题
- Python help
- 聚簇索引与非聚簇索引的区别