2012-08-03 101 views
0

我想爲Apache poi中的工作簿/ excel動態生成多個工作表。我想知道如何以高效且線程安全/併發的方式進行操作。如何在apache poi中動態生成多個工作表

  1. 因此,多個工作表動態地提供命名選項。
  2. 每個工作表都有自己的一組列(etc)(或樣式)。
  3. 寫一個servlet等

請幫助返回這些回來。

謝謝。

+0

[忙開發人員指南HSSF和XSSF特性]( http://poi.apache.org/spreadsheet/quick-guide.html)提到'工作簿WB =新HSSFWorkbook(); //或新的XSSFWorkbook(); Sheet sheet1 = wb.createSheet(「new sheet」); Sheet sheet2 = wb.createSheet(「second sheet」); '這不適合你嗎? – gresdiplitude 2012-08-03 05:46:14

回答

0

是是這樣嗎?

public static void createExcel(String excelFilePath, String sheetName) 
     throws IOException { 
    FileOutputStream fos = null; 
    try { 
     HSSFWorkbook workbook = null; 
     if (new File(excelFilePath).createNewFile()) { 
      workbook = new HSSFWorkbook(); 
     } else { 
      POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream(
        new File(excelFilePath))); 
      workbook = new HSSFWorkbook(pfs); 
     } 
     if (workbook.getSheet(sheetName) == null) { 
      fos = new FileOutputStream(excelFilePath); 
      workbook.createSheet(sheetName); 
      workbook.write(fos); 
     } 

    } catch (IOException e) { 
     throw e; 
    } finally { 
     if (fos != null) { 
      fos.close(); 
     } 
    } 
} 
+0

感謝您的回答,但這似乎不正確。 – Nomad 2012-08-03 02:04:26

0

請找到示例代碼。

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Date; 
import java.util.List; 
import java.util.Map; 

import org.apache.poi.hssf.usermodel.HSSFCellStyle; 
import org.apache.poi.hssf.usermodel.HSSFFont; 
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.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 

public class ExcelUtility { 

    public static boolean writeDataSheetWise(final String excelFileName, final List<String> headers, 
      Map<String, List<Object[]>> sheetRowDataList) throws IOException, InvalidFormatException { 

     boolean isWritten = false; 
     HSSFWorkbook workbook = new HSSFWorkbook(); 
     for(String sheetName: sheetRowDataList.keySet()) { 
      createSheet(workbook, sheetName, headers, sheetRowDataList.get(sheetName)); 
     } 

     try { 
      System.out.println("\nWritting data to excel file <" + excelFileName + ">"); 

      FileOutputStream outputStream = new FileOutputStream(new File(excelFileName)); 
      workbook.write(outputStream); 
      outputStream.flush(); 
      outputStream.close(); 
      isWritten = true; 

      System.out.println("\nData is successfully written to excel file <"+excelFileName+">."); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return isWritten; 
    } 

    public static boolean writeData(final String excelFileName, final String sheetName, final List<String> headers, 
      List<Object[]> rowDataList) throws IOException, InvalidFormatException { 

     boolean isWritten = false; 
     HSSFWorkbook workbook = new HSSFWorkbook(); 
     createSheet(workbook, sheetName, headers, rowDataList); 

     try { 
      System.out.println("\nWritting data to excel file <" + excelFileName + ">"); 

      FileOutputStream outputStream = new FileOutputStream(new File(excelFileName)); 
      workbook.write(outputStream); 
      outputStream.flush(); 
      outputStream.close(); 
      isWritten = true; 

      System.out.println("\nData is successfully written to excel file <"+excelFileName+">."); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return isWritten; 

    } 

    @SuppressWarnings("deprecation") 
    private static void createSheet(final HSSFWorkbook workbook, final String sheetName, final List<String> headers, 
      final List<Object[]> rowDataList) { 

     HSSFSheet sheet = workbook.createSheet(sheetName); 

     int rowCount = 0; 

     HSSFCellStyle style = workbook.createCellStyle(); 
     HSSFFont headersFont = workbook.createFont(); 
     headersFont.setFontName(HSSFFont.FONT_ARIAL); 
     headersFont.setFontHeightInPoints((short) 16); 
     headersFont.setColor(HSSFColor.GREEN.index); 
     headersFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
     style.setFont(headersFont); 

     // Creating header row 
     Row headerRow = sheet.createRow(rowCount++); 
     for (int i = 0; i < headers.size(); i++) { 
      Cell cell = headerRow.createCell(i); 
      cell.setCellStyle(style); 
      cell.setCellValue(headers.get(i)); 
      sheet.autoSizeColumn(i); 
     } 

     for (Object rowDataObject[] : rowDataList) { 
      Row row = sheet.createRow(rowCount++); 
      int cellnum = 0; 
      for (Object rowData : rowDataObject) { 
       Cell cell = row.createCell(cellnum++); 
       if (rowData instanceof Date) 
        cell.setCellValue((Date) rowData); 
       else if (rowData instanceof Boolean) 
        cell.setCellValue((Boolean) rowData); 
       else if (rowData instanceof String) 
        cell.setCellValue((String) rowData); 
       else if (rowData instanceof Integer) 
        cell.setCellValue((Integer) rowData); 
       else if (rowData instanceof Long) 
        cell.setCellValue((Long) rowData); 
       else if (rowData instanceof Double) 
        cell.setCellValue((Double) rowData); 

      } 
     } 
    } 
} 
相關問題