使用Open XML
在excel文件中写入数据后出现错误错误是“Excel中找不到不可读的内容。您要恢复此工作簿的内容吗?”
以下是我用于使用Open XML读取/写入Excel文件的Code Snipped。
部分代码来自MSDN网站。
Private Function GetWorksheetPartByName(sheetName As String) As WorksheetPart
Dim sheets As IEnumerable(Of Sheet) = _document.WorkbookPart.Workbook.GetFirstChild(Of Sheets)().Elements(Of Sheet)().Where(Function(s) s.Name = sheetName)
If sheets.Count() = 0 Then
' The specified worksheet does not exist.
_logger.Error(String.Format("Worksheet {0} not found in {1}", sheetName, _fileName))
'Throw New Exception(String.Format("Worksheet {0} not found in {1}", sheetName, _fileName))
'MessageBox.Show(String.Format("Worksheet {0} not found in {1}", sheetName, _fileName), "Data Export", MessageBoxButtons.OK)
If Not chkIgnoreSheetNotFoundMsg.Checked Then
Dim customError As New CustomError
customError.FileLocation = _fileName
customError.PosAndMsg.Add(sheetName, Helper.SHEET_NOT_FOUND)
errors.Add(customError)
End If
Throw New SheetNotFoundException(sheetName, _fileName)
End If
Dim relationshipId As String = sheets.First().Id.Value
Dim worksheetPart As WorksheetPart = _document.WorkbookPart.GetPartById(relationshipId)
Return worksheetPart
End Function
Private Function InsertCellInWorksheet(ByVal columnName As String, ByVal rowIndex As UInteger, ByVal worksheetPart As WorksheetPart) As Cell
Dim worksheet As Worksheet = worksheetPart.Worksheet
Dim sheetData As SheetData = worksheet.GetFirstChild(Of SheetData)()
Dim cellReference As String = (columnName + rowIndex.ToString())
' If the worksheet does not contain a row with the specified row index, insert one.
Dim row As Row
If (sheetData.Elements(Of Row).Where(Function(r) r.RowIndex.Value = rowIndex).Count() <> 0) Then
row = sheetData.Elements(Of Row).Where(Function(r) r.RowIndex.Value = rowIndex).First()
Else
row = New Row()
row.RowIndex = rowIndex
sheetData.Append(row)
End If
' If there is not a cell with the specified column name, insert one.
If (row.Elements(Of Cell).Where(Function(c) c.CellReference.Value = columnName + rowIndex.ToString()).Count() > 0) Then
Return row.Elements(Of Cell).Where(Function(c) c.CellReference.Value = cellReference).First()
Else
' Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
Dim refCell As Cell = Nothing
For Each cell As Cell In row.Elements(Of Cell)()
If (String.Compare(cell.CellReference.Value, cellReference, True) = 0) Then
refCell = cell
Exit For
End If
Next
Dim newCell As Cell = New Cell
newCell.CellReference = cellReference
row.InsertBefore(newCell, refCell)
worksheet.Save()
Return newCell
End If
End Function
Private Sub UpdateCell(text As String, rowIndex As UInteger, columnName As String)
' Open the document for editing.
Dim worksheetPart As WorksheetPart = GetWorksheetPartByName(_sheetName)
If worksheetPart IsNot Nothing Then
Dim cell As Cell = InsertCellInWorksheet(columnName, rowIndex, worksheetPart)
cell.CellValue = New CellValue(text)
Dim val As Double
If Double.TryParse(text, val) Then
cell.DataType = New EnumValue(Of CellValues)(CellValues.Number)
Else
cell.DataType = New EnumValue(Of CellValues)(CellValues.String)
End If
' Save the worksheet.
worksheetPart.Worksheet.Save()
End If
End Sub
Private Function GetCell(columnName As String, rowIndex As UInteger) As Cell
Dim worksheet As Worksheet = GetWorksheetPartByName(_sheetName).Worksheet
Dim sheetData As SheetData = worksheet.GetFirstChild(Of SheetData)()
' If the worksheet does not contain a row with the specified row index, insert one.
Dim row As Row
If (sheetData.Elements(Of Row).Where(Function(r) r.RowIndex.Value = rowIndex).Count() <> 0) Then
row = sheetData.Elements(Of Row).Where(Function(r) r.RowIndex.Value = rowIndex).First()
Else
row = New Row()
row.RowIndex = rowIndex
sheetData.Append(row)
End If
If row Is Nothing Then
Return Nothing
End If
Return row.Elements(Of Cell)().Where(Function(c) String.Compare(c.CellReference.Value, columnName & rowIndex, True) = 0).FirstOrDefault()
End Function
Private Function GetCellValue(columnName As String, rowIndex As Integer) As String
Dim stringTable = _document.WorkbookPart.GetPartsOfType(Of SharedStringTablePart)().FirstOrDefault()
Dim c As Cell = GetCell(columnName, rowIndex)
Dim text As String = String.Empty
If c IsNot Nothing Then
If c.DataType IsNot Nothing Then
Select Case c.DataType.Value
Case CellValues.SharedString
If stringTable IsNot Nothing Then
text = stringTable.SharedStringTable.ElementAt(Integer.Parse(c.InnerText)).InnerText
End If
Exit Select
Case CellValues.Number
text = c.InnerText
Exit Select
Case CellValues.[Boolean]
Select Case c.InnerText
Case "0"
text = "FALSE"
Exit Select
Case Else
text = "TRUE"
Exit Select
End Select
Exit Select
Case Else
text = c.CellValue.InnerText
Exit Select
End Select
Else
If c.CellFormula IsNot Nothing Then
text = c.CellValue.InnerText
Else
text = c.InnerText
End If
End If
End If
Return text
End Function
我在循环中调用UpdateCell方法,用于在Excel工作表中写入值。我也使用GetCellValue方法从工作表中读取值。
代码工作正常,但在打开文件时,它会在“ Excel中找到不可读的内容。您要恢复此工作簿的内容吗?
每个文件都没有发生。我只在特定文件中遇到此问题。