poi包-小结(用于导出excel,word)
目录
- A.poi导出excel设置某一列为下拉框
- B.操作小结
- C.设置单元格颜色
A.poi导出excel设置某一列为下拉框
1.首先,必须是poi3.5以上的包,若是提示nosuchmethod,可能是编译包和运行包不同导致的
使用:System.out.println("+++---+++"+HSSFWorkbook.class.getProtectionDomain().getCodeSource().getLocation());
查看运行时使用包的名称。
2.实现代码
String[] textlist = {"是","否"};
sheet = setHSSFValidation(sheet, textlist, 2, 500, 2, 2);// 第三列的前501行都设置为选择列表形式.
四个参数分别是:起始行、终止行、起始列、终止列
public static HSSFSheet setHSSFValidation(HSSFSheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) { // 加载下拉列表内容 DVConstraint constraint = DVConstraint.createExplicitListConstraint(textlist); // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); // 数据有效性对象 HSSFDataValidation data_validation_list = new HSSFDataValidation(regions, constraint); sheet.addValidationData(data_validation_list); return sheet; }

B.操作小结
先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); HSSFCellStyle setBorder = wb.createCellStyle(); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); HSSFCellStyle setBorder = wb.createCellStyle();
一、设置背景色: HSSFCellStyle goldBoldRedStyle = wb.createCellStyle(); goldBoldRedStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); goldBoldRedStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());
二、设置边框: setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
三、设置居中: setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 //垂直居中 HSSFCellStyle centerH = wb.createCellStyle(); centerH.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平方向的对齐方式 centerH.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直方向的对齐方式
四、设置字体: HSSFFont font = wb.createFont(); font.setFontName("黑体"); font.setFontHeightInPoints((short) 16);//设置字体大小 HSSFFont font2 = wb.createFont(); font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 font2.setFontHeightInPoints((short) 12); setBorder.setFont(font);//选择需要用到的字体格式 HSSFFont font = wb.createFont(); font.setFontName("黑体"); font.setFontHeightInPoints((short) 16);//设置字体大小 HSSFFont font2 = wb.createFont(); font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 font2.setFontHeightInPoints((short) 12); setBorder.setFont(font);//选择需要用到的字体格式
五、设置列宽: sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值 HSSFRow row = sheet.createRow((short) 1); row.setHeight((short) 450);//目的是想把行高设置成450px sheet.setColumnWidth((short) i, (short) 4800);//设置列宽
六、设置自动换行: HSSFCellStyle wrapStyle = wb.createCellStyle(); wrapStyle.setWrapText(true);//设置自动换行
七、合并单元格: Region region1 = new Region(0, (short) 0, 0, (short) 6); //参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号 sheet.addMergedRegion(region1); Region region1 = new Region(0, (short) 0, 0, (short) 6); //参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号 sheet.addMergedRegion(region1); 例子:sheet.addMergedRegion(new Region(0,(short)0,0,(short)(13))); //合并单元格
C.设置单元格颜色

package com.java.connect.poi; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; 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.xssf.usermodel.XSSFWorkbook; public class POIFillAndColorExample { public static void main(String[] args) throws IOException { // Create a workbook object Workbook workbook = new XSSFWorkbook(); // Create sheet Sheet sheet = workbook.createSheet(); // Create a row and put some cells in it. Row row = sheet.createRow((short) 1); // Aqua background CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.AQUA.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); Cell cell = row.createCell((short) 1); cell.setCellValue("X1"); cell.setCellStyle(style); // Orange "foreground", foreground being the fill foreground not the // font color. style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row.createCell((short) 2); cell.setCellValue("X2"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row.createCell((short) 3); cell.setCellValue("X3"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row.createCell((short) 4); cell.setCellValue("X4"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row.createCell((short) 5); cell.setCellValue("X5"); cell.setCellStyle(style); // Create a row and put some cells in it. Row row2 = sheet.createRow((short) 2); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.BROWN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row2.createCell((short) 1); cell.setCellValue("X6"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.CORAL.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row2.createCell((short) 2); cell.setCellValue("X7"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row2.createCell((short) 3); cell.setCellValue("X8"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row2.createCell((short) 4); cell.setCellValue("X9"); cell.setCellStyle(style);
style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row2.createCell((short) 5); cell.setCellValue("X10"); cell.setCellStyle(style); // Create a row and put some cells in it. Row row3 = sheet.createRow((short) 3); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.DARK_RED.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row3.createCell((short) 1); cell.setCellValue("X11"); cell.setCellStyle(style);
style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row3.createCell((short) 2); cell.setCellValue("X12"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row3.createCell((short) 3); cell.setCellValue("X13"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.GOLD.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row3.createCell((short) 4); cell.setCellValue("X14"); cell.setCellStyle(style); style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row3.createCell((short) 5); cell.setCellValue("X15"); cell.setCellStyle(style); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("POIFillAndColorExample.xlsx"); workbook.write(fileOut); fileOut.close(); } }

浙公网安备 33010602011771号