如何使用Open Xml 2.5 SDK创建(导出)只读excel文件



我正在开发一个WPF应用程序(使用MVVM)。我还可以将我的列表导出到Excel文件。但我的问题是,我无法将其只读,因此没有人能够在之后进行更改。

document.WorkbookPart.Workbook.WorkbookProtection=new WorkbookProtection
                                                          {
                                                               LockStructure=true
                                                          };

我只找到了这个,但它只是使工作簿只读

这是我创建excel文件的代码

public static bool CreateExcelDocument(DataSet ds, string excelFilename)
    {
        try
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Create(excelFilename, SpreadsheetDocumentType.Workbook))
            {
                document.AddWorkbookPart();
                document.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
                document.WorkbookPart.Workbook.WorkbookProtection=new WorkbookProtection
                                                                      {
                                                                          LockStructure = true,
                                                                      };
                //  My thanks to James Miera for the following line of code (which prevents crashes in Excel 2010)
                document.WorkbookPart.Workbook.Append(new BookViews(new WorkbookView()));
                //  If we don't add a "WorkbookStylesPart", OLEDB will refuse to connect to this .xlsx file !
                WorkbookStylesPart workbookStylesPart = document.WorkbookPart.AddNewPart<WorkbookStylesPart>("rIdStyles");
                Stylesheet stylesheet = new Stylesheet();
                workbookStylesPart.Stylesheet = stylesheet;
                CreateParts(ds, document);
            }
            Trace.WriteLine("Successfully created: " + excelFilename);
            return true;
        }
        catch (Exception ex)
        {
            Trace.WriteLine("Failed, exception thrown: " + ex.Message);
            return false;
        }
    }
    private static void CreateParts(DataSet ds, SpreadsheetDocument spreadsheet)
    {
        //  Loop through each of the DataTables in our DataSet, and create a new Excel Worksheet for each.
        uint worksheetNumber = 1;
        foreach (DataTable dt in ds.Tables)
        {
            //  For each worksheet you want to create
            string workSheetID = "rId" + worksheetNumber.ToString();
            string worksheetName = dt.TableName;
            WorksheetPart newWorksheetPart = spreadsheet.WorkbookPart.AddNewPart<WorksheetPart>();
            newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();
            // create sheet data
            newWorksheetPart.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());
            // save worksheet
            WriteDataTableToExcelWorksheet(dt, newWorksheetPart);
            newWorksheetPart.Worksheet.Save();
            // create the worksheet to workbook relation
            if (worksheetNumber == 1)
                spreadsheet.WorkbookPart.Workbook.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheets());
            spreadsheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
            {
                Id = spreadsheet.WorkbookPart.GetIdOfPart(newWorksheetPart),
                SheetId = (uint)worksheetNumber,
                Name = dt.TableName
            });
            worksheetNumber++;
        }
        spreadsheet.WorkbookPart.Workbook.Save();
    }

在打开Excel之前,您可以使用锁定文件

FileInfo cInfo = new FileInfo(Path);
cInfo.IsReadonly = true;

它将是只读的。

相关内容

最新更新