Java Poi操作Excel写入数据并设置style
2018-01-21 14:15
741 查看
1、向Excel中写入数据,并再追加一遍相同的数据
ExcelWriter.writeToExcel方法可以向指定excel表中写入mapList中的数据;
ExcelWriter.addToExcel方法可以向指定excel表中追加mapLit中的数据;
package excelUtils; import java.io.*; import java.util.*; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelWriter { private static XSSFWorkbook workbook = null; public static void main(String[] args) { String fileDir = "D:/test.xlsx"; String sheetName = "sheet1"; String[] titleRow = { "title1", "title2", "title3", "title4", "title5", "title6" 20000 }; List<Map<String, String>> mapList = new ArrayList<Map<String, String>>(); for (int i = 0; i < 5; i++) { Map<String, String> map = new HashMap<String, String>(); map.put(titleRow[0], "第" + (i + 1) + "行第1列"); map.put(titleRow[1], "第" + (i + 1) + "行第2列"); map.put(titleRow[2], "第" + (i + 1) + "行第3列"); map.put(titleRow[3], "第" + (i + 1) + "行第4列"); map.put(titleRow[4], "第" + (i + 1) + "行第5列"); map.put(titleRow[5], "第" + (i + 1) + "行第6列"); mapList.add(map); } writeToExcel(fileDir, sheetName, titleRow, mapList); addToExcel(fileDir, sheetName, mapList); } /** *根据标题,mapList写入制定excel表中 * *@param fileDir *@param sheetName *@param titleRow *@param mapList */ public static void writeToExcel(String fileDir, String sheetName, String[] titleRow, List<Map<String, String>> mapList) { if (!ExcelCreater.fileExist(fileDir)) { ExcelCreater.createExcel(fileDir, sheetName, titleRow); } if (!ExcelCreater.sheetExist(fileDir, sheetName)) { ExcelCreater.createSheet(fileDir, sheetName, titleRow); } writeToExcelRow(fileDir, sheetName, mapList, 1); } /** *从第rowId行开始将mapList写入 * *@param fileDir *@param sheetName *@param mapList *@param rowId */ public static void writeToExcelRow(String fileDir, String sheetName, List<Map<String, String>> mapList, int rowId) { File file = new File(fileDir); FileOutputStream out = null; try { workbook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet sheet = workbook.getSheet(sheetName); // 获取表格的总行数 // introwCount = sheet.getLastRowNum() + 1; // 需要加一 // 获取表头的列数 int columnCount = sheet.getRow(0).getLastCellNum(); XSSFRow titleRow = sheet.getRow(0); if (titleRow != null) { for (Map<String, String> map : mapList) { XSSFRow newRow = sheet.createRow(rowId++); for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { String mapKey = titleRow.getCell(columnIndex).toString().trim(); XSSFCell cell = newRow.createCell(columnIndex); cell.setCellValue(map.get(mapKey) == null ? "" : map.get(mapKey).toString()); } } } out = new FileOutputStream(fileDir); workbook.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** *添加到已存在excel * *@param fileDir *@param sheetName *@param mapList */ public static void addToExcel(String fileDir, String sheetName, List<Map<String, String>> mapList) { File file = new File(fileDir); FileOutputStream out = null; try { workbook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet sheet = workbook.getSheet(sheetName); int rowId = sheet.getLastRowNum() + 1; // 获取表格的总行数 int columnCount = sheet.getRow(0).getLastCellNum();// 获取表头的列数 XSSFRow titleRow = sheet.getRow(0); if (titleRow != null) { for (Map<String, String> map : mapList) { XSSFRow newRow = sheet.createRow(rowId++); for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { String mapKey = titleRow.getCell(columnIndex).toString().trim(); XSSFCell cell = newRow.createCell(columnIndex); cell.setCellValue(map.get(mapKey) == null ? "" : map.get(mapKey).toString()); } } } out = new FileOutputStream(fileDir); workbook.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** *向指定单元格写入数据 * *@param fileDir *@param sheetName *@param value *@param rowId *@param columnId *@throws Exception */ public static void writeToExcelCell(String fileDir, String sheetName, String value, int rowId, int columnId) { File file = new File(fileDir); FileOutputStream out = null; try { workbook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet sheet = workbook.getSheet(sheetName); XSSFRow row = sheet.getRow(rowId); if (row != null) { XSSFCell cell = row.getCell(columnId); if (cell != null) { cell.setCellValue(value == null ? null : value); } else { XSSFCell newCell = row.createCell(columnId); newCell.setCellValue(value == null ? null : value); } } out = new FileOutputStream(fileDir); workbook.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } } |
2、写入结果
3、设置style
package excelUtils; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; public class ExcelStyle { private static XSSFWorkbook workbook = null; private static Color light_green = new Color(198, 224, 180); private static Color light_orange = new Color(248, 203, 173); private static Color light_blue = new Color(180, 198, 231); private static Color light_yellow = new Color(255, 230, 153); private static Color light_gray = new Color(217, 217, 217); public static void main(String[] args){ String fileDir = "D:/test.xlsx"; String sheetName = "sheet1"; int[] columnWidth = { 10, 20, 30, 10, 20, 30 };//每列列宽 setExcelSimpleStyle(fileDir, sheetName, columnWidth); } /** *只设置列宽, 颜色按默认绿橙蓝排列,居中 * *@param fileDir *@param sheetName *@param columnWidth *@return */ public static boolean setExcelSimpleStyle(String fileDir, String sheetName, int[] columnWidth) { String[] colors = { "light_green", "light_orange", "light_blue" }; boolean inCenter = true; if (!ExcelCreater.fileExist(fileDir)) { return false; } if (!ExcelCreater.sheetExist(fileDir, sheetName)) { return false; } setStyle(fileDir, sheetName, columnWidth, colors, inCenter); return true; } /** *判断是否存在,设置列宽,颜色,居中,返回是否设置成功 * *@param fileDir *@param sheetName *@param columnWidth *@param colors *@param inCenter */ public static boolean setExcelStyle(String fileDir, String sheetName, int[] columnWidth, String[] colors, boolean inCenter) { if (!ExcelCreater.fileExist(fileDir)) { return false; } if (!ExcelCreater.sheetExist(fileDir, sheetName)) { return false; } setStyle(fileDir, sheetName, columnWidth, colors, inCenter); return true; } /** *设置列宽,颜色,居中 * *@param fileDir *@param sheetName *@param columnWidth *@param colors *@param inCenter */ public static void setStyle(String fileDir, String sheetName, int[] columnWidth, String[] colors, boolean inCenter) { FileOutputStream out = null; File file = new File(fileDir); try { workbook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet sheet = workbook.getSheet(sheetName); setColumnWidth(sheet, columnWidth); int rowCount = sheet.getLastRowNum() + 1; int columnCount = sheet.getRow(0).getLastCellNum(); Map<Integer, XSSFCellStyle> styleMap = new HashMap<>(); for (int colorIndex = 0; colorIndex < colors.length; colorIndex++) { XSSFCellStyle style = getStyle(colors[colorIndex], inCenter, null, 11); styleMap.put(colorIndex, style); } for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { XSSFRow newRow = sheet.getRow(rowIndex); for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { XSSFCell cell = newRow.getCell(columnIndex); cell.setCellStyle(styleMap.get(columnIndex % styleMap.size())); } } out = new FileOutputStream(fileDir); workbook.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** *设置每行高度 * *@param sheet *@param rowHeight */ public static void setRowHeight(XSSFSheet sheet, int rowHeight) { sheet.setDefaultRowHeight((short) rowHeight); } /** *分别设置每一列宽度,若只有一个值,全部设为此值 * *@param sheet *@param columnWidth */ public static void setColumnWidth(XSSFSheet sheet, int[] columnWidth) { if (columnWidth.length == 1) { sheet.setDefaultColumnWidth(columnWidth[0]); } else { for (int i = 0; i < columnWidth.length; i++) { sheet.setColumnWidth(i, columnWidth * 256); } } } /** *获得制定颜色字体居中的style * *@param color *@param inCenter *@param fontName *@param fontHeight */ public static XSSFCellStyle getStyle(String color, boolean inCenter, String fontName, int fontHeight) { XSSFCellStyle style = [i]workbook.createCellStyle(); if (color != null && !color.equals("")) { setStyleColor(style, color); } if (inCenter == true) { setAlignment(style); } if (fontName != null && !fontName.equals("")) { setStyleFont(style, fontName, fontHeight); } return style; } /** *设置style的颜色,目前五种颜色可选 * *@param style *@param color */ public static void setStyleColor(XSSFCellStyle style, String color) { if (color.equals("light_green")) { style.setFillForegroundColor(new XSSFColor(light_green)); } if (color.equals("light_orange")) { style.setFillForegroundColor(new XSSFColor(light_orange)); } if (color.equals("light_blue")) { style.setFillForegroundColor(new XSSFColor(light_blue)); } if (color.equals("light_yellow")) { style.setFillForegroundColor(new XSSFColor(light_yellow)); } if (color.equals("light_gray")) { style.setFillForegroundColor(new XSSFColor(light_gray)); } style.setFillPattern(FillPatternType.SOLID_FOREGROUND); } /** *设置style字体 * *@param style *@param fontName *@param fontHeight */ public static void setStyleFont(XSSFCellStyle style, String fontName, int fontHeight) { XSSFFont font = workbook.createFont(); font.setFontName(fontName); font.setFontHeight(fontHeight); style.setFont(font); } /** *设置style水平垂直居中 * *@param style */ public static void setAlignment(XSSFCellStyle style) { style.setAlignment(HorizontalAlignment.CENTER);// 水平居中 style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中 } } |
4、最终结果
相关文章推荐
- Java通过POI技术操作Excel(3)----数据导出
- POI实战-java开发excel详解(第四章 常用操作-单元格合并与数据读取)
- apache POI3.2 java操作excel 设置数据有效性,实现excel单元格列表和提示
- POI对Excel操作——java读取Excel数据
- java 对excel操作 读取、写入、修改数据;导出数据库数据到excel
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- java操作Excel之POI(5)利用POI实现使用模板批量导出数据
- 14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式
- java poi技术操作excel之写Excel(向Excel中写入值)
- java操作Excel之POI(6)使用POI实现使用模板批量添加数据
- POI实战-java开发excel详解之常用操作-单元格合并与数据读取
- 用Java POI操作Excel,读取数据导入DB2数据库
- java 使用poi 操作excel设置缩放比例
- Java操作Excel之理解JXL--读取Excel模板动态写入数据并生成Excel
- java操作poi如何更改excel中的数据
- Java程序员从笨鸟到菜鸟之(一百零五)java操作office和pdf文件(三)利用jxl实现数据导出excel报表以及与POI的区别
- java使用poi把从数据库中取出的数据写入到excel文件中并保存到指定文件路径
- java中使用JXL对Excel文件进行数据的写入、导出操作
- java SpringMVC中 POI读取数据库数据并写入Excel表格中,并实现下载功能