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