翻譯|使用教程|編輯:李顯亮|2020-09-16 11:07:54.663|閱讀 623 次
概述:Aspose.Cells提供了一個(gè) 代表Microsoft Excel文件的類Workbook。該工作簿 類包含一個(gè)工作表 集合允許訪問每個(gè)工作表中的Excel文件。工作表由Worksheet 類表示。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務(wù),支持構(gòu)建具有生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印電子表格功能的跨平臺(tái)應(yīng)用程序。
在接下來的系列教程中,將為開發(fā)者帶來Aspose.Cells for .NET的一系列使用教程,例如關(guān)于加載保存轉(zhuǎn)換、字體、渲染、繪圖、智能標(biāo)記等等。本文重點(diǎn)介紹如何設(shè)置打印選項(xiàng)。
>>Aspose.Cells for .NET已經(jīng)更新至v20.9,支持獲取/設(shè)置切片器形狀屬性,新增客戶端api用于添加/刪除GridWeb的注釋,凍結(jié)和拆分工作表的功能增強(qiáng),點(diǎn)擊下載體驗(yàn)
Aspose.Cells提供了一個(gè)表示Microsoft Excel文件的類--Workbook,該類包含一個(gè)工作表集合,允許訪問Excel文件中的每個(gè)工作表。Workbook類包含一個(gè)Worksheets集合,允許訪問Excel文件中的每個(gè)工作表。一個(gè)工作表由Worksheet類來表示。
Worksheet類提供了Protect方法,用于對工作表進(jìn)行保護(hù)。Protect方法接受以下參數(shù)。
該P(yáng)rotectionType 枚舉包含以下預(yù)先定義的保護(hù)類型:
保護(hù)類型 | 描述 |
---|---|
All | 用戶無法修改此工作表上的任何內(nèi)容 |
內(nèi)容 | 用戶無法在此工作表中輸入數(shù)據(jù) |
對象 | 用戶無法修改圖形對象 |
情境 | 用戶無法修改已保存的方案 |
結(jié)構(gòu)體 | 用戶無法修改結(jié)構(gòu) |
視窗 | 保護(hù)已應(yīng)用于Windows |
None | 未應(yīng)用保護(hù) |
下面的示例顯示了如何使用密碼保護(hù)工作表。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // Creating a file stream containing the Excel file to be opened FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); // Instantiating a Workbook object // Opening the Excel file through the file stream Workbook excel = new Workbook(fstream); // Accessing the first worksheet in the Excel file Worksheet worksheet = excel.Worksheets[0]; // Protecting the worksheet with a password worksheet.Protect(ProtectionType.All, "aspose", null); // Saving the modified Excel file in default format excel.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003); // Closing the file stream to free all resources fstream.Close();
在上面的代碼用于保護(hù)工作表之后,您可以通過打開工作表來檢查保護(hù)。打開文件并嘗試向工作表中添加一些數(shù)據(jù)后,您將看到以下對話框:
要在工作表上工作,請選擇“ 保護(hù)”,然后從“ 工具”菜單項(xiàng)中選擇“取消保護(hù)工作表”來取消保護(hù)工作表。選擇“取消保護(hù)工作表”菜單項(xiàng)后,將打開一個(gè)對話框,提示您輸入密碼,以便您可以在工作表上進(jìn)行操作,如下所示:
在此方法中,我們僅使用Aspose.Cells API來完成任務(wù)。
示例:以下示例展示了如何保護(hù)工作表中的一些單元格。它首先解鎖工作表中的所有單元格,然后鎖定其中的3個(gè)單元格(A1,B1,C1)。最后,它保護(hù)了工作表。該樣式對象包含了一個(gè)布爾屬性,IsLocked。您可以將IsLocked 屬性設(shè)置為true或false,然后應(yīng)用Column / Row.ApplyStyle()方法來鎖定或解鎖具有所需屬性的行/列。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // Create directory if it is not already present. bool IsExists = System.IO.Directory.Exists(dataDir); if (!IsExists) System.IO.Directory.CreateDirectory(dataDir); // Create a new workbook. Workbook wb = new Workbook(); // Create a worksheet object and obtain the first sheet. Worksheet sheet = wb.Worksheets[0]; // Define the style object. Style style; // Define the styleflag object StyleFlag styleflag; // Loop through all the columns in the worksheet and unlock them. for (int i = 0; i <= 255; i++) { style = sheet.Cells.Columns[(byte)i].Style; style.IsLocked = false; styleflag = new StyleFlag(); styleflag.Locked = true; sheet.Cells.Columns[(byte)i].ApplyStyle(style, styleflag); } // Lock the three cells...i.e. A1, B1, C1. style = sheet.Cells["A1"].GetStyle(); style.IsLocked = true; sheet.Cells["A1"].SetStyle(style); style = sheet.Cells["B1"].GetStyle(); style.IsLocked = true; sheet.Cells["B1"].SetStyle(style); style = sheet.Cells["C1"].GetStyle(); style.IsLocked = true; sheet.Cells["C1"].SetStyle(style); // Finally, Protect the sheet now. sheet.Protect(ProtectionType.All); // Save the excel file. wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn