POI 输出Excel

    技术2022-05-12  4

    package cc.dynasoft.struts.action;import java.io.FileOutputStream;import java.io.IOException;import java.util.Iterator;import java.util.List;import java.util.Vector;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFFooter;import org.apache.poi.hssf.usermodel.HSSFHeader;import org.apache.poi.hssf.usermodel.HSSFPrintSetup;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.hssf.util.Region;import cc.dynasoft.bean.Department;///import org.apache.poi.hssf.record.HeaderRecorder; public class OutputExcel { public static boolean outputExcel(ExcelArgs args, List title, List list) {  try {   int cellNum = args.getCellNum(); // workbook    int rowNum = args.getRowNum();   /**    * 建立表格设置。    */   HSSFWorkbook wb = new HSSFWorkbook(); // create the new Workbook    HSSFSheet sheet = wb.createSheet(args.getSheetName()); // create    /**    * 打印设置    */   HSSFPrintSetup hps = sheet.getPrintSetup();   hps.setPaperSize((short9); // 设置A4纸    // hps.setLandscape(true); // 将页面设置为横向打印模式    sheet.setHorizontallyCenter(true); // 设置打印页面为水平居中    // sheet.setVerticallyCenter(true); // 设置打印页面为垂直居中    wb.setPrintArea(0"$A$2:$e$" + rowNum + 2);// 打印区域设置.    /**    * 设置表的Footer    */   HSSFFooter footer = sheet.getFooter();   // 设置footer的位置和显示的内容    footer.setCenter("Time:" + HSSFFooter.date());   footer.setRight("Page " + HSSFFooter.page() + " of "     + HSSFFooter.numPages());   /**    * 设置表的Header    */   // 设置header的位置,共有三种位置和相应的显示设置    HSSFHeader header = sheet.getHeader();   // header.setRight("Center Header");    // header.setLeft("Left Header");    header.setCenter(HSSFHeader.font("Stencil-Normal""Italic")     + HSSFHeader.fontSize((short30) + args.getHeaderTitle());   // header.endDoubleUnderline();    header.startUnderline();   /**    * 设置列的宽度    */   sheet.setColumnWidth((short2,     (short) ((30 * 8) / ((double1 / 10)));   sheet.setColumnWidth((short3,     (short) ((40 * 8) / ((double1 / 10)));   sheet.setColumnWidth((short4,     (short) ((50 * 8) / ((double1 / 20)));   /**    * 创建第一行,也就是显示的标题, 可以高置的高度,单元格的格式,颜色,字体等设置. 同时可以合并单元格.    */   HSSFRow row0 = sheet.createRow(0); // 创建0行    row0.setHeight((short0x300); // 设直行的高度.    HSSFFont font2 = wb.createFont(); // 创建字体格式    font2.setColor(HSSFFont.SS_NONE); // 设置单元格字体的颜色.    font2.setFontHeight((short700); // 设置字体大小    font2.setFontName("Courier New"); // 设置单元格字体    HSSFCell cell0 = row0.createCell((short0); // 创建0行0列.    HSSFCellStyle style3 = wb.createCellStyle(); // 创建单元格风格.    style3.setAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中    style3.setAlignment(HSSFCellStyle.ALIGN_CENTER); // /水平居中    style3.setFont(font2); // 将字体格式加入到单元格风格当中    // cell0.setCellType()    cell0.setCellStyle(style3); // 设置单元格的风格.    cell0.setCellValue(args.getHeaderTitle()); // 设置单元的内容.    sheet.addMergedRegion(new Region(0, (short00,     (short) (cellNum - 1)));// 指定合并区域,前二个参数为开始处X,Y坐标.后二个为结束的坐标.    /**    * 设置其它数据 设置风格    */   HSSFCellStyle style = wb.createCellStyle();   style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单无格的边框为粗体    style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);   style.setLeftBorderColor(HSSFColor.BLACK.index);   style.setBorderRight(HSSFCellStyle.BORDER_THIN);   style.setRightBorderColor(HSSFColor.BLACK.index);   style.setBorderTop(HSSFCellStyle.BORDER_THIN);   style.setTopBorderColor(HSSFColor.BLACK.index);   // style.setWrapText(true);//文本区域随内容多少自动调整    // style.setFillForegroundColor(HSSFColor.LIME.index);    // style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);    /**    * 设置风格1    */   HSSFCellStyle style1 = wb.createCellStyle();   style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单无格的边框为粗体    style1.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.    style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);   style1.setLeftBorderColor(HSSFColor.BLACK.index);   style1.setBorderRight(HSSFCellStyle.BORDER_THIN);   style1.setRightBorderColor(HSSFColor.BLACK.index);   style1.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);   style1.setTopBorderColor(HSSFColor.BLACK.index);   style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 最好的设置Pattern    // 单元格背景的显示模式.    style1.setFillForegroundColor(new HSSFColor.RED().getIndex()); // 设置单元格背景色;    style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平对齐方式    // style1.setWrapText(true);//文本区域随内容多少自动调整    // style.setFillPattern(HSSFCellStyle.//);    // 设置字体Color,首先创建Font对象,后对font设置,然后做为参数传给style    HSSFFont font = wb.createFont();   font.setColor(HSSFFont.SS_NONE);   // font.setFontHeightInPoints((short)24);    font.setFontName("Courier New");   // font.setItalic(true);    // font.setStrikeout(true);//给字体加上删除线    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   style1.setFont(font);   /**    *     * 设置第零行表格说明行    *     *     *     */   HSSFRow row1 = sheet.createRow((short1);   for (int j = 0; j < cellNum; j++) {    HSSFCell cell = row1.createCell((short) j);    cell.setCellValue((String) title.get(j));    cell.setCellStyle(style1);   }   // style.setFillPattern(HSSFCellStyle.NO_FILL);    /**    * 设置表的内容主体    */   Iterator iter = list.iterator();   for (int i = 2; iter.hasNext(); i++) {    Department dep = (Department) iter.next();    HSSFRow row = sheet.createRow((short) i);    HSSFCell cell5 = row.createCell((short0);    HSSFCell cell1 = row.createCell((short1);    HSSFCell cell2 = row.createCell((short2);    HSSFCell cell3 = row.createCell((short3);    HSSFCell cell4 = row.createCell((short4);    cell5.setCellValue(dep.getId());    cell5.setCellStyle(style);    cell1.setCellValue(dep.getParentId());    cell1.setCellStyle(style);    cell2.setCellValue(dep.getName());    cell2.setCellStyle(style);    cell3.setCellValue(dep.getDescription());    cell3.setCellStyle(style);    cell4.setCellValue(dep.getImagePath());    cell4.setCellStyle(style);   }   // Write the output to a file}    // FileOutputStream fileOut = new    // FileOutputStream(args.getPath()+args.getFileName());    /**    * 对文件进行输出操作。    */   FileOutputStream fileOut = new FileOutputStream(args     .getPathAndName());   wb.write(fileOut);   // fileOut.close();   } catch (IOException ex) {   ex.printStackTrace();  } catch (Exception ex) {   ex.printStackTrace();  }  return true; }}

    最新回复(0)