public static void writeExcel (ExcelParam excelParam) throws IOException { // Criar um workbook Workbook wb = new HSSFWorkbook (); // Criar uma planilha Sheet sheet = wb.createSheet ("sheet1"); // Set font Font font = wb. createFont (); font.setColor (HSSFFont.COLOR_NORMAL); // cor da fonte font.setFontName ("Microsoft Yahei"); // font font.setFontHeightInPoints ((short) 18); // font.setItalic (true1); / / Se deve usar itálico // font.setStrikeout (true); // Se deve usar traços // Definir o estilo da célula CellStyle cellStyle = wb.createCellStyle (); cellStyle.setBorderBottom (BorderStyle.THIN); // Borda inferior cellStyle .setBorderLeft (BorderStyle.THIN); // Borda esquerda cellStyle.setBorderTop (BorderStyle.THIN); // Borda superior CellStyle sh = wb.createCellStyle (); cellStyle.setBorderRight (BorderStyle.THIN); // Borda direita cellStyle.setFont (font); cellStyle.setAlignment (HorizontalAlignment.CENTER); // Layout horizontal: centralizado // Definir o alinhamento vertical do conteúdo da célula para o centro cellStyle.setVerticalAlignment ( VerticalAlignment.CENTER); // cellStyle.setWrapText (true ); // Envolva automaticamente // Cria uma linha de cabeçalho Row rowHead = sheet.createRow (0); // Preencha o conteúdo do cabeçalho // Preencha o cabeçalho com dados Font fh = wb .createFont (); fh.setColor (HSSFFont.COLOR_RED); // Cor da fonte fh.setFontName ("黑体"); // Fonte fh.setBold (true); fh.setFontHeightInPoints ((curto) 22); sh .setBorderBottom (BorderStyle.THIN); // Borda inferior sh.setBorderLeft (BorderStyle.THIN); // Borda esquerda sh.setBorderTop (BorderStyle.THIN); // borda superior sh.setBorderRight (BorderStyle.THIN); // borda direita sh.setFont (fh); sh.setAlignment ( HorizontalAlignment.CENTER); // Layout horizontal: Centralizado // Define o conteúdo da célula a ser alinhado verticalmente sh.setVerticalAlignment (VerticalAlignment.CENTER); for (int i = 0; i <excelParam.getHead (). Size (); i ++ ) { // Mesclar a primeira linha da célula sheet.addMergedRegion (new CellRangeAddress (0,0,0, excelParam.getData (). Get (0) .size () - 1)); Cell cell = rowHead.createCell ( i); cell.setCellValue (excelParam.getHead (). get (i)); cell.setCellStyle (sh); cell.setCellType (STRING); } // Lista de análise do corpo principal do formulário tabela // Conteúdo da tabela para (int i = 0; i <excelParam.getData (). Size (); i ++) {// Número de linhas Row row = sheet.createRow (i + 1); for (int j = 0; j <excelParam.getData (). Get (i) .size (); j ++) {// Número de colunas // As colunas se adaptam automaticamente a sheet.autoSizeColumn (j); Cell cell = row.createCell (j); cell.setCellValue (excelParam.getData (). get (i) .get (j)); cell.setCellStyle (cellStyle); cell.setCellType (STRING); } } // Determine se existe um diretório. Arquivo arquivo = novo Arquivo (excelParam.getFilePath ()); if (! file.getParentFile (). exists ()) { // Criar arquivo.mkdirs (); } // Arquivo Excel de saída 1 FileOutputStream output = new FileOutputStream (arquivo); wb.write (output); // 写入 磁盘 output.close (); }
O EXCEL gera miosótis (cor da fonte, alinhamento, tamanho, células de mesclagem)
Acho que você gosta
Origin blog.csdn.net/a1_HelloWord/article/details/84729197
Recomendado
Clasificación