2010-10-28 51 views
1

任何人都可以幫我解決這個問題。POI:2000及以上記錄的單元格格式受損

我使用的是vb.net 2003,我試圖在使用興趣點的Excel中生成報告,但當記錄高於2000時,單元格格式在下一個2000年缺失或損壞時出現問題,以上記錄。當我打開生成的報告並顯示一條消息「對於許多不同的單元格格式」和下一條消息是「Excel遇到錯誤,必須刪除一些格式以避免工作簿被破壞。仔細檢查你的格式。「

任何人都可以幫助我解決這個問題,或者任何人都可以爲我提供另一個想法來格式化所有的單元格,不管它是2000年以上的記錄。

下面的代碼是我的示例代碼。

Private Sub btnExtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtract.Click 

     Try 
      Dim source As String = Application.StartupPath & "\Sample Template.xls" 
      Dim sfdialog As New SaveFileDialog 

      'Save File 
      sfdialog.Filter = "Excel File | *.xls" 
      If sfdialog.ShowDialog = DialogResult.OK Then 

       'Variable Decleration 
       Dim fileIn As java.io.InputStream = New java.io.FileInputStream(source) 
       Dim fileOut As java.io.OutputStream = New java.io.FileOutputStream(sfdialog.FileName) 
       Dim wb As HSSFWorkbook = New HSSFWorkbook(fileIn) 
       Dim sheet As HSSFSheet = wb.getSheet("Sample Template") 
       Dim row As HSSFRow 
       Dim cell As HSSFCell 
       Dim fileName As String = sfdialog.FileName 

       'Inputs Data 
       For rowNum As Integer = 3 To 10000 
        row = createRowCell(sheet, rowNum) 
        cell = row.getCell(1) 
        cell.setCellValue(rowNum - 2) 
        cell = row.getCell(2) 
        cell.setCellValue("EN" & rowNum - 2) 
        cell = row.getCell(3) 
        cell.setCellValue("TN" & rowNum - 2) 
       Next 

       fileIn.close() 
       wb.write(fileOut) 
       fileOut.flush() 
       fileOut.close() 

       If fileName <> "" Then 
        If MessageBox.Show("Open Generated Excel?", "Open File", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then 
         Try 
          Process.Start(fileName) 
         Catch ex As Exception 
          MessageBox.Show("Cannot open file", "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
         End Try 
        End If 
       End If 
      End If 
     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     End Try 


    End Sub 


    Private Function createRowCell(ByRef sheet As HSSFSheet, ByVal rowIndex As Int32) As HSSFRow 
     Dim cellIndex As Int32 = 1 
     Dim newRow As HSSFRow = sheet.createRow(rowIndex) 
     Dim commonStyle As HSSFCellStyle = sheet.getWorkbook().createCellStyle() 
     Dim dateStyle As HSSFCellStyle = sheet.getWorkbook().createCellStyle() 
     Dim createHelper As HSSFCreationHelper = sheet.getWorkbook().getCreationHelper() 
     Dim newCell As HSSFCell 

     commonStyle.setBorderBottom(1) 
     commonStyle.setBorderTop(1) 
     commonStyle.setBorderLeft(1) 
     commonStyle.setBorderRight(1) 

     newCell = newRow.createCell(cellIndex) 
     newCell.setCellStyle(commonStyle) 
     cellIndex += 1 
     newCell = newRow.createCell(cellIndex) 
     newCell.setCellStyle(commonStyle) 
     cellIndex += 1 
     newCell = newRow.createCell(cellIndex) 
     newCell.setCellStyle(commonStyle) 


     Return newRow 
    End Function 

回答

0

它現在工作!只是檢查你的格式,如果它不是多餘的。就像格式化一個單元格的邊界一樣(E.G.右,下,左,右)。對每個單元格只設置頂部和左側邊框的格式非常好,然後在行或列的末尾添加一些邊框格式(EG在底部添加邊框(如果它到達最大行),並在到達最大行的情況下在右側添加邊框小區的最後一欄):d

下面是對應的代碼:

Try 
      Dim source As String = Application.StartupPath & "\Sample Template.xls" 
      Dim sfdialog As New SaveFileDialog 

      'Save File 
      sfdialog.Filter = "Excel File | *.xls" 
      If sfdialog.ShowDialog = DialogResult.OK Then 

       'Variable Decleration 
       Dim fileIn As java.io.InputStream = New java.io.FileInputStream(source) 
       Dim fileOut As java.io.OutputStream = New java.io.FileOutputStream(sfdialog.FileName) 
       Dim wb As HSSFWorkbook = New HSSFWorkbook(fileIn) 
       Dim sheet As HSSFSheet = wb.getSheet("Sample Template") 
       Dim row As HSSFRow 
       Dim cell As HSSFCell 

       Dim bgStyle As HSSFCellStyle = wb.createCellStyle 
       Dim rightBorder As HSSFCellStyle = wb.createCellStyle 
       Dim fileName As String = sfdialog.FileName 
       Dim records As Integer = 60000 
       'Inputs Data 

       bgStyle.setBorderTop(1) 
       bgStyle.setBorderLeft(1) 
       rightBorder.setBorderTop(1) 
       rightBorder.setBorderLeft(1) 
       rightBorder.setBorderRight(1) 

       For rowNum As Integer = 3 To records 
        row = sheet.createRow(rowNum) 

        If rowNum = records Then bgStyle.setBorderBottom(1) 
        '1 
        cell = row.createCell(1) 
        cell.setCellValue(rowNum - 2) 
        cell.setCellStyle(bgStyle) 

        cell = row.createCell(2) 
        cell.setCellValue("EN" & rowNum - 2) 
        cell.setCellStyle(bgStyle) 

        cell = row.createCell(3) 
        cell.setCellValue("TN" & rowNum - 2) 
        cell.setCellStyle(bgStyle) 

       Next 

       fileIn.close() 
       wb.write(fileOut) 
       fileOut.flush() 
       fileOut.close() 

       If fileName <> "" Then 
        If MessageBox.Show("Open Generated Excel?", "Open File", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then 
         Try 
          Process.Start(fileName) 
         Catch ex As Exception 
          MessageBox.Show("Cannot open file", "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
         End Try 
        End If 
       End If 
      End If 
     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     End Try 
1

這可能不是POI問題。根據this Microsoft KB article,可以在一個工作簿中定製的格式數量有限制。文章介紹了幾個解決方案(真正的解決方法)。希望你能弄清楚一些事情。

+0

感謝您的信息周杰倫,我現在已經一個想法,以什麼樣的錯誤是。我有一個想法,您可以再次鏈接到REOPEN工作簿以繼續進行更多格式設置。但是,我不知道如何重新打開代碼或將代碼寫入當前正在處理的工作簿以繼續格式化其他單元格。你能幫我做這件事情嗎? – Bryan 2010-10-28 08:51:58