POI

xiaoxiao2026-06-06  8

package poi.metrics; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFFont; 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; public class TestExecl { /** * 創建單元格 */ public static void createCell(HSSFRow row, int column, HSSFCellStyle style, int cellType, Object value) { HSSFCell cell = row.createCell((short) column); cell.setEncoding(HSSFCell.ENCODING_UTF_16); if (style != null) { cell.setCellStyle(style); } switch (cellType) { case HSSFCell.CELL_TYPE_BLANK: { } break; case HSSFCell.CELL_TYPE_STRING: { if (value != null) cell.setCellValue(value.toString()); } break; case HSSFCell.CELL_TYPE_NUMERIC: { if (value != null) { cell.setCellValue(Double.parseDouble(value.toString())); } } break; default: break; } } /** * 粗體樣式 */ private static HSSFCellStyle CreateBoldStyle(HSSFWorkbook wb) { HSSFFont boldFont = wb.createFont(); boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); boldFont.setFontHeight((short) 230); HSSFCellStyle style = wb.createCellStyle(); style.setFont(boldFont); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderTop((short) 1); style.setBorderBottom((short) 1); style.setBorderLeft((short) 1); style.setBorderRight((short) 1); style.setWrapText(true); return style; } private static HSSFCellStyle newStyle(HSSFWorkbook wb) { HSSFFont boldFont = wb.createFont(); boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); boldFont.setFontHeight((short) 230); HSSFCellStyle style = wb.createCellStyle(); style.setFont(boldFont); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderTop((short) 1); style.setBorderBottom((short) 1); style.setBorderLeft((short) 1); style.setBorderRight((short) 1); style.setWrapText(true); HSSFDataFormat format = wb.createDataFormat(); style.setDataFormat(format.getFormat("0.00%")); return style; } /** * @param args */ public static void main(String[] args) { FileOutputStream fileOut; try { fileOut = new FileOutputStream("poi_excel.xls"); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("DRME_Sheet"); sheet.setColumnWidth((short) 0, (short) 3000); sheet.setColumnWidth((short) 1, (short) 4000); sheet.setColumnWidth((short) 2, (short) 4000); // 第一行標題樣式 HSSFCellStyle topStyle = CreateBoldStyle(wb); createCell(sheet.createRow(0), 0, topStyle, HSSFCell.CELL_TYPE_STRING, "vo.getCompanyCode()"); createCell(sheet.createRow(1), 1, topStyle, HSSFCell.CELL_TYPE_STRING, "vo.getCompany()"); // BigDecimal bg =new BigDecimal("1"); // BigDecimal bg2 =new BigDecimal("30"); // System.out.println(bg.divide(bg2,2,BigDecimal.ROUND_HALF_UP)); // System.out.println(bg.multiply(bg2)); createCell(sheet.createRow(2), 2, newStyle(wb), HSSFCell.CELL_TYPE_NUMERIC, new BigDecimal("22.222")); wb.write(fileOut); fileOut.flush(); fileOut.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 相关资源:Java POI 导入导出Excel简单实例源代码
转载请注明原文地址: https://www.6miu.com/read-5049691.html

最新回复(0)