通过poi的XSSF实现生成excel文件
2017-10-26 17:23
239 查看
maven导入依赖jar包:
java代码:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.6</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.6</version> </dependency>
java代码:
import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Demo { public static void main(String[] args) { // 文件内容 List<Object[]> rows = new ArrayList<Object[]>(); rows.add(new String[] { "编号", "姓名", "成绩" }); rows.add(new Object[] { 1001, "张三", 87.5F }); rows.add(new Object[] { 1002, "李四", 99.5F }); rows.add(new Object[] { 1003, "王五", null }); rows.add(new Object[] { 1004, "小六", 59F }); // 文件路径 String folderPath = "E:\\tmp"; // 文件名称 String fileName = "学生分数"; // 生成文件 new Demo().createExcelFile(rows, folderPath, fileName); } /** * 根据[文件内容&文件路径&文件名称],生成一个excel文件 */ private void createExcelFile(List<Object[]> rows, String folderPath, String fileName) { try { // 创建一个Workbook XSSFWorkbook wb = new XSSFWorkbook(); // 创建一个Sheet XSSFSheet sheet = wb.createSheet(fileName); // 样式1:设置列宽 sheet.setColumnWidth(0, 2000); // 第一列的宽度为2000 sheet.setColumnWidth(1, 3000); // 第二列的宽度为3000 // 样式1:设置单元格背景 CellStyle titleStyle = wb.createCellStyle(); titleStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex()); titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 样式1:设置单元格边框 titleStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框 titleStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框 titleStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框 titleStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框 // 样式1:设置单元格字体 Font font = wb.createFont(); // font.setColor((short) 42); // 设置字体颜色 font.setColor(HSSFColor.GREEN.index); // XSSFColor中未找到颜色和short数值的映射,使用HSSFColor来定位颜色的short值 font.setFontName("黑体"); // 设置字体 font.setFontHeightInPoints((short) 12);// 设置字体大小 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗体显示 titleStyle.setFont(font); // 样式1:设置单元格居中 titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 样式2:设置单元格居中 CellStyle contentStyle = wb.createCellStyle(); contentStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 遍历输出每行 for (int i = 0; i < rows.size(); i++) { // 创建一个row XSSFRow row = sheet.createRow(i); // 每一行的数据 Object[] rowData = rows.get(i); // 遍历生成每个单元格 for (int j = 0; j < rowData.length; j++) { // 创建一个cell XSSFCell cell = row.createCell(j); // 样式:设置单元格样式 if (i == 0) { cell.setCellStyle(titleStyle);// 使用样式1 } else { cell.setCellStyle(contentStyle);// 使用样式2 } // 如果为空,就不做设值处理 if (null == rowData[j]) { continue; } // 设值处理:假设只有四种类型的数据,如果还有其他类型,根据需要,做格式转换 // String类型数值 if (rowData[j].getClass() == String.class) { cell.setCellValue((String) rowData[j]); } // double类型数值 else if (rowData[j].getClass() == double.class || rowData[j].getClass() == Double.class) { cell.setCellValue((Double) rowData[j]); } // float类型数值 else if (rowData[j].getClass() == float.class || rowData[j].getClass() == Float.class) { cell.setCellValue((Float) rowData[j]); } // integer类型数值 else if (rowData[j].getClass() == int.class || rowData[j].getClass() == Integer.class) { cell.setCellValue((Integer) rowData[j]); } } } // 文件路径 String filePath = folderPath + File.separator + fileName + ".xls";// 含文件名的全路径,如果是2007及以后的版本,后缀可用.xlsx,向前兼容 File file = new File(filePath); // 如果父目录不存在,创建父目录 if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // 如果已存在,删除旧文件 if (file.exists()) { file.delete(); } // 将excel内容写入到文件当中 file.createNewFile(); FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- POI实现生成excel文件
- 实现poi方式生成excel文件和web类根据路径生成java实例的反射技术的springmvc方法的方法
- 利用POI读入excel通过反射生成Bean源码实现
- OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码)
- 通过poi实现解析并读取excel文件(包含xls、xlsx后缀)
- 通过poi实现解析并读取excel文件(包含xls、xlsx后缀)
- OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql) 学习笔记(含源代码)
- 本文使用c#通过oledb可获取excel文件表的结构信息,同理也可以获得access等数据库的表结构信息,实现如下:
- 使用JAVA通过JXL(JExcel)动态生成Excel文件 转载至台湾某博客
- 通过excel可识别的xml结构直接生成xls文件收藏
- poi 实现 java生成excel
- 使用POI生成Excel文件,可以自动调整excel列宽等
- C# 通过文件结构直接生成xls(Excel)文件
- POI实现Excel文件上传下载,读/写数据库(poi)
- WSDL2Java通过WSDL文件生成java服务接口并实现
- 通过生成PSR文件,实现动态报表格式
- Java通过POI读写Excel文件——于晓昆
- velocity基础教程--4通过velocity生成文件--网站静态化实现方案
- 一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)
- 使用POI生成Excel文件及使用POI在EXCEL文件中插入图片的方法