3

我正在开发一个 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();
    }
4

1 回答 1

1

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

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

比它将是只读的。

于 2013-08-09T09:35:29.190 回答