Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打开由Open XML创建的Excel文件时出错_Xml_Excel_Openxml - Fatal编程技术网

打开由Open XML创建的Excel文件时出错

打开由Open XML创建的Excel文件时出错,xml,excel,openxml,Xml,Excel,Openxml,我已经开始使用OpenXML创建Excel文件,以便向其中添加一些数据。我包含的示例文件是我在网上找到的示例的修改版本。它创建了excel文件,但当我试图打开它时,它给了我三个错误。我怀疑最后两个错误是由于为标题单元格添加了StyleIndex,但第一个错误最近在我打开任何Excel文件时出现 1-无法使用对象链接和嵌入 2-Excel在“SheetPInfo.xlsx”中发现无法读取的内容。是否恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是” 3-Excel能够通过修复或删除不可读的

我已经开始使用OpenXML创建Excel文件,以便向其中添加一些数据。我包含的示例文件是我在网上找到的示例的修改版本。它创建了excel文件,但当我试图打开它时,它给了我三个错误。我怀疑最后两个错误是由于为标题单元格添加了StyleIndex,但第一个错误最近在我打开任何Excel文件时出现

1-无法使用对象链接和嵌入

2-Excel在“SheetPInfo.xlsx”中发现无法读取的内容。是否恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是”

3-Excel能够通过修复或删除不可读的内容来打开文件。 修复的记录:来自/xl/worksheets/sheet.xml部分的单元格信息

在关闭最后一条错误消息后,我还获得了文件中的数据/数组,但是样式索引(大字体)没有出现在标题单元格“lastname”中

可能出了什么问题?当我在网上搜索时,行的顺序对于Excel非常重要,所以我想我的StyleIndex相关行可能放错了位置

任何帮助都将不胜感激

Imports System.Linq
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Public Class TestEcel10
    Inherits System.Web.UI.Page

    Private mFileName As String = "SheetPInfo.xlsx"
    Private mFileLocation As String = "c:\Contracts\" & mFileName
    Private newSpreadSheet As SpreadsheetDocument

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Private Sub btnExportToExcel_Click(sender As Object, e As EventArgs) Handles btnExportToExcel.Click

        Dim wsp As WorksheetPart = Spreadsheet_Create()

        Dim PInfoArr(2, 3) As String
        PInfoArr(0, 0) = "Frank"
        PInfoArr(0, 1) = "Baker"
        PInfoArr(0, 2) = "2/18/1977"
        PInfoArr(0, 3) = "Frank"

        PInfoArr(1, 0) = "William"
        PInfoArr(1, 1) = "Cook"
        PInfoArr(1, 2) = "2/18/1987"
        PInfoArr(1, 3) = "Bill"

        PInfoArr(2, 0) = "Nancy"
        PInfoArr(2, 1) = "Williams"
        PInfoArr(2, 2) = "2/18/1973"
        PInfoArr(2, 3) = "Nancy"

        InsertText(wsp, PInfoArr)
        Spreadsheet_Close()
    End Sub   'End of btnExportToExcel_Click Sub

    Private Function Spreadsheet_Create() As WorksheetPart
        'Delete the file if exists
        If My.Computer.FileSystem.FileExists(mFileLocation) Then
            My.Computer.FileSystem.DeleteFile(mFileLocation)
        End If

        'Create spreadsheet
        newSpreadSheet = SpreadsheetDocument.Create(mFileLocation, SpreadsheetDocumentType.Workbook)

        'Add WorkbookPart & Workbook
        Dim newWorkbookPart As WorkbookPart = newSpreadSheet.AddWorkbookPart
        newWorkbookPart.Workbook = New Workbook()

        'Add Worksheet
        Return InsertWorksheet(newSpreadSheet.WorkbookPart)
    End Function   'End of Spreadsheet_Create function

    Private Sub Spreadsheet_Close()
        newSpreadSheet.WorkbookPart.Workbook.Save()
        newSpreadSheet.Close()
    End Sub   'End of Spreadsheet_Close sub

    Public Sub InsertText(ByVal wsp As WorksheetPart, ByVal PInfoArray As String(,))
        ' Get the SharedStringTablePart. If it does not exist, create a new one.
        Dim shareStringPart As SharedStringTablePart
        If (newSpreadSheet.WorkbookPart.GetPartsOfType(Of SharedStringTablePart).Count() > 0) Then
            shareStringPart = newSpreadSheet.WorkbookPart.GetPartsOfType(Of SharedStringTablePart).First()
        Else
            shareStringPart = newSpreadSheet.WorkbookPart.AddNewPart(Of SharedStringTablePart)()
        End If

        Dim index As Integer = 0
        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim cell As Cell

        'Inserting column headings:

        ' Insert the text into the SharedStringTablePart.
        index = InsertSharedStringItem("First Name", shareStringPart)
        ' Insert cell A1 into the new worksheet.
        cell = InsertCellInWorksheet("A", 1, wsp)
        ' Set the value of cell A1.
        cell.CellValue = New CellValue(index.ToString)
        cell.DataType = New EnumValue(Of CellValues)(CellValues.SharedString)

        ' Insert the text into the SharedStringTablePart.
        index = InsertSharedStringItem("Last Name", shareStringPart)
        Dim styleIndex As UInt32Value = Convert.ToInt32("36")
        ' Insert cell A1 into the new worksheet.
        cell = InsertCellInWorksheet("B", 1, wsp)
        ' Set the value of cell A1.
        cell.CellValue = New CellValue(index.ToString)
        cell.StyleIndex = styleIndex
        cell.DataType = New EnumValue(Of CellValues)(CellValues.SharedString)

        ' Insert the text into the SharedStringTablePart.
        index = InsertSharedStringItem("Birth Date", shareStringPart)
        ' Insert cell A1 into the new worksheet.
        cell = InsertCellInWorksheet("C", 1, wsp)
        ' Set the value of cell A1.
        cell.CellValue = New CellValue(index.ToString)
        cell.DataType = New EnumValue(Of CellValues)(CellValues.SharedString)

        ' Insert the text into the SharedStringTablePart.
        index = InsertSharedStringItem("Nick Name", shareStringPart)
        ' Insert cell A1 into the new worksheet.
        cell = InsertCellInWorksheet("D", 1, wsp)
        ' Set the value of cell A1.
        cell.CellValue = New CellValue(index.ToString)
        cell.DataType = New EnumValue(Of CellValues)(CellValues.SharedString)

        'For column data we start from index 2 and increase it as we go to the next row
        Dim Rowindex As Integer = 2
        Dim ColumnLetter As String = "A"

        'Inserting column data:
        For i = 0 To UBound(PInfoArray, 1)

            For j = 0 To UBound(PInfoArray, 2)

                ' Insert the text into the SharedStringTablePart.
                index = InsertSharedStringItem(PInfoArray(i, j), shareStringPart)
                'Set the 
                If j = 0 Then
                    ColumnLetter = "A"
                ElseIf j = 1 Then
                    ColumnLetter = "B"
                ElseIf j = 2 Then
                    ColumnLetter = "C"
                ElseIf j = 3 Then
                    ColumnLetter = "D"
                End If
                ' Insert cell A1 into the new worksheet.
                cell = InsertCellInWorksheet(ColumnLetter, Rowindex, wsp)
                ' Set the value of cell A1.
                cell.CellValue = New CellValue(index.ToString)
                cell.DataType = New EnumValue(Of CellValues)(CellValues.SharedString)

                If ColumnLetter = "D" Then
                    Rowindex = Rowindex + 1
                End If
            Next
        Next

        ' Save the new worksheet.
        wsp.Worksheet.Save()
    End Sub   'End of InsertText sub

    ' Given text and a SharedStringTablePart, creates a SharedStringItem with the specified text
    ' and inserts it into the SharedStringTablePart. If the item already exists, returns its index.
    Private Function InsertSharedStringItem(ByVal text As String, ByVal shareStringPart As SharedStringTablePart) As Integer
        'If the part does not contain a SharedStringTable, create one
        If (shareStringPart.SharedStringTable Is Nothing) Then
            shareStringPart.SharedStringTable = New SharedStringTable
        End If

        Dim i As Integer = 0
        'Iterate through all the items in the SharedStringTable. If the text already exists, return its index
        For Each item As SharedStringItem In shareStringPart.SharedStringTable.Elements(Of SharedStringItem)()
            If (item.InnerText = text) Then
                Return i
            End If
            i = (i + 1)
        Next

        'The text does not exist in the part. Create the SharedStringItem and return its index
        shareStringPart.SharedStringTable.AppendChild(New SharedStringItem(New DocumentFormat.OpenXml.Spreadsheet.Text(text)))
        shareStringPart.SharedStringTable.Save()
        Return i
    End Function   'End of InsertSharedStringItem function

    'Given a WorkbookPart, inserts a new worksheet.
    Private Function InsertWorksheet(ByVal workbookPart As WorkbookPart) As WorksheetPart

        ' Add a new worksheet part to the workbook.
        Dim newWorksheetPart As WorksheetPart = _
                       workbookPart.AddNewPart(Of WorksheetPart)()
        newWorksheetPart.Worksheet = New Worksheet(New SheetData)
        newWorksheetPart.Worksheet.Save()

        'Get a unique ID for the new worksheet
        Dim sheetId As UInteger = 1
        Dim sheets As New Sheets
        If newSpreadSheet.WorkbookPart.Workbook.GetFirstChild(Of Sheets)() IsNot Nothing Then
            sheets = newSpreadSheet.WorkbookPart.Workbook.GetFirstChild(Of Sheets)()
            'Not sure if this check is required, possible an hanger-on from previous attempts
            If (sheets.Elements(Of Sheet).Count > 0) Then
                sheetId = sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1
            End If
        End If

        Dim relationshipId As String = _
                       newSpreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart)
        Dim sheetName As String = ("Sheet" + sheetId.ToString())

        ' Add the new worksheet and associate it with the workbook.
        Dim sheet As Sheet = New Sheet
        sheet.Id = relationshipId
        sheet.SheetId = sheetId
        sheet.Name = sheetName
        sheets.Append(sheet)
        If newSpreadSheet.WorkbookPart.Workbook.GetFirstChild(Of Sheets)() Is Nothing Then
            newSpreadSheet.WorkbookPart.Workbook.AppendChild(Of Sheets)(sheets)
        End If
        workbookPart.Workbook.Save()

        Return newWorksheetPart

    End Function   'End of InsertWorksheet function

    ' Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet.
    ' If the cell already exists, return it.
    Private Function InsertCellInWorksheet(ByVal columnName As String, ByVal rowIndex As UInteger, ByVal wsp As WorksheetPart) As Cell
        Dim worksheet As Worksheet = wsp.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   'End of InsertCellInWorksheet function
导入系统.Linq
导入DocumentFormat.OpenXml
导入DocumentFormat.OpenXml.Packaging
导入DocumentFormat.OpenXml.Spreadsheet
公共类测试10
继承System.Web.UI.Page
私有mFileName为String=“SheetPInfo.xlsx”
私有mFileLocation为String=“c:\Contracts\”&mFileName
作为电子表格文档的私人新闻预告表
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
端接头
私有子btnExportToExcel\u Click(发件人作为对象,e作为事件参数)处理btnExportToExcel。单击
将wsp设置为工作表零件=电子表格_创建()
作为字符串的Dim PINFORAR(2,3)
PInfoArr(0,0)=“Frank”
PInfoArr(0,1)=“Baker”
PInfoArr(0,2)=“2/18/1977”
PInfoArr(0,3)=“Frank”
PInfoArr(1,0)=“威廉”
PInfoArr(1,1)=“厨师”
PInfoArr(1,2)=“2/18/1987”
PInfoArr(1,3)=“账单”
PInfoArr(2,0)=“Nancy”
PInfoArr(2,1)=“Williams”
PInfoArr(2,2)=“2/18/1973”
PInfoArr(2,3)=“Nancy”
InsertText(wsp、PInfoArr)
电子表格(u Close)
“结束子项”结束btnExportToExcel\u单击子项
专用函数电子表格\u Create()作为工作表部件
'删除文件(如果存在)
如果存在My.Computer.FileSystem.files(mFileLocation),则
My.Computer.FileSystem.DeleteFile(mFileLocation)
如果结束
'创建电子表格
newSpreadSheet=SpreadsheetDocument.Create(mFileLocation,SpreadsheetDocumentType.工作簿)
'添加工作簿部件和工作簿
将newWorkbookPart设置为WorkbookPart=newSpreadSheet.AddWorkbookPart
newWorkbookPart.工作簿=新工作簿()
'添加工作表
返回插入工作表(newSpreadSheet.WorkbookPart)
结束函数“电子表格结束”\u创建函数
专用子电子表格_Close()
newSpreadSheet.WorkbookPart.Workbook.Save()文件
newSpreadSheet.Close()
“结束子项”电子表格的结束\u结束子项
Public Sub InsertText(ByVal wsp作为工作表部分,ByVal PInfoArray作为字符串(,))
'获取SharedStringTablePart。如果它不存在,请创建一个新的。
将shareStringPart设置为SharedStringTablePart
如果(newSpreadSheet.WorkbookPart.GetPartsOfType)(SharedStringTablePart的).Count()>0),则
shareStringPart=newSpreadSheet.WorkbookPart.GetPartSoftType(属于SharedStringTablePart.First())
其他的
shareStringPart=newSpreadSheet.WorkbookPart.AddNewPart(属于SharedStringTablePart)()
如果结束
作为整数的Dim索引=0
尺寸i为整数=0
尺寸j为整数=0
作为细胞的暗淡细胞
'插入列标题:
'将文本插入SharedStringTablePart。
index=InsertSharedStringItem(“名字”,shareStringPart)
'将单元格A1插入新工作表。
单元格=InsertCellInWorksheet(“A”,1,wsp)
'设置单元格A1的值。
cell.CellValue=新的CellValue(index.ToString)
cell.DataType=新的枚举值(共个CellValues)(CellValues.SharedString)
'将文本插入SharedStringTablePart。
index=InsertSharedStringItem(“姓氏”,shareStringPart)
Dim styleIndex As UINT32 VALUE=Convert.ToInt32(“36”)
'将单元格A1插入新工作表。
单元格=插入工作表(“B”,1,wsp)
'设置单元格A1的值。
cell.CellValue=新的CellValue(index.ToString)
cell.StyleIndex=StyleIndex
cell.DataType=新的枚举值(共个CellValues)(CellValues.SharedString)
'将文本插入SharedStringTablePart。
索引=InsertSharedStringItem(“出生日期”,shareStringPart)
'将单元格A1插入新工作表。
单元格=InsertCellInWorksheet(“C”,1,wsp)
'设置单元格A1的值。
cell.CellValue=新的CellValue(index.ToString)
cell.DataType=新的枚举值(共个CellValues)(CellValues.SharedString)
'将文本插入SharedStringTablePart。
索引=InsertSharedStringItem(“昵称”,shareStringPart)
'将单元格A1插入新工作表。
单元格=插入工作表(“D”,1,wsp)
'设置单元格A1的值。
cell.CellValue=新的CellValue(index.ToString)
cell.DataType=新的枚举值(共个CellValues)(CellValues.SharedString)
'对于列数据,我们从索引2开始,在进入下一行时增加它
Dim Rowindex为整数=2
Dim ColumnLetter As String=“A”
'插入列数据:
对于i=0到uBond(PInfoArray,1)
对于j=0到uBond(PInfoArray,2)
'将文本插入SharedStringTablePart。