翻譯|使用教程|編輯:李顯亮|2020-04-26 09:20:21.497|閱讀 1163 次
概述:Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務,支持構建具有生成,修改,轉換,呈現和打印電子表格功能的跨平臺應用程序。 本文重點介紹如何復制和移動工作表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務,支持構建具有生成,修改,轉換,呈現和打印電子表格功能的跨平臺應用程序。
在接下來的系列教程中,將為開發者帶來Aspose.Cells for .NET的一系列使用教程,例如關于加載保存轉換、字體、渲染、繪圖、智能標記等等。本文重點介紹如何復制和移動工作表。
>>Aspose.Cells for .NET已經更新至v20.4,支持多個單元作為范圍的并集,添加用于更新PowerQueryFormulaItems的源字段的選項,支持ODS的數據欄,色標和圖標集條件格式,修復諸多Bug,點擊下載體驗
有時,需要大量具有通用格式和數據的工作表。例如,如果使用季度預算,則可能要創建一個工作簿,其工作表包含相同的列標題,行標題和公式。有一種方法可以做到:創建一張紙,然后復制它。
Aspose.Cells支持在工作簿內部或之間復制和移動工作表。包含數據,格式,表格,矩陣,圖表,圖像和其他對象的工作表將以最高的精確度進行復制。
使用Aspose.Cells在工作簿中復制工作表
Aspose.Cells提供了一個重載方法Aspose.Cells.WorksheetCollection.AddCopy(),該方法用于將工作表添加到集合中并從現有工作表中復制數據。該方法的一種版本將源工作表的索引作為參數。另一個版本采用源工作表的名稱。
下面的示例演示如何在工作簿中復制現有工作表。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string InputPath = dataDir + "book1.xls"; // Open an existing Excel file. Workbook wb = new Workbook(InputPath); // Create a Worksheets object with reference to // the sheets of the Workbook. WorksheetCollection sheets = wb.Worksheets; // Copy data to a new sheet from an existing // sheet within the Workbook. sheets.AddCopy("Sheet1"); // Save the Excel file. wb.Save(dataDir + "CopyWithinWorkbook_out.xls");
在工作簿之間復制工作表
Aspose.Cells提供了一種Aspose.Cells.Worksheet.Copy()方法, 用于將數據和格式從源工作表復制到工作簿內部或工作簿之間。該方法將源工作表對象作為參數。
下面的示例演示如何將工作表從一個工作簿復制到另一工作簿。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string InputPath = dataDir + "book1.xls"; // Create a Workbook. // Open a file into the first book. Workbook excelWorkbook0 = new Workbook(InputPath); // Create another Workbook. Workbook excelWorkbook1 = new Workbook(); // Copy the first sheet of the first book into second book. excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]); // Save the file. excelWorkbook1.Save(dataDir + "CopyWorksheetsBetweenWorkbooks_out.xls");
下面的示例演示如何將工作表從一個工作簿復制到另一個。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); // Create a new Workbook. Workbook excelWorkbook0 = new Workbook(); // Get the first worksheet in the book. Worksheet ws0 = excelWorkbook0.Worksheets[0]; // Put some data into header rows (A1:A4) for (int i = 0; i < 5; i++) { ws0.Cells[i, 0].PutValue(string.Format("Header Row {0}", i)); } // Put some detail data (A5:A999) for (int i = 5; i < 1000; i++) { ws0.Cells[i, 0].PutValue(string.Format("Detail Row {0}", i)); } // Define a pagesetup object based on the first worksheet. PageSetup pagesetup = ws0.PageSetup; // The first five rows are repeated in each page... // It can be seen in print preview. pagesetup.PrintTitleRows = "$1:$5"; // Create another Workbook. Workbook excelWorkbook1 = new Workbook(); // Get the first worksheet in the book. Worksheet ws1 = excelWorkbook1.Worksheets[0]; // Name the worksheet. ws1.Name = "MySheet"; // Copy data from the first worksheet of the first workbook into the // first worksheet of the second workbook. ws1.Copy(ws0); // Save the excel file. excelWorkbook1.Save(dataDir + "CopyWorksheetFromWorkbookToOther_out.xls");
在工作簿中移動工作表
Aspose.Cells提供了一種Aspose.Cells.Worksheet.MoveTo()方法,該方法用于將工作表移動到同一電子表格中的另一個位置。該方法將目標工作表索引作為參數。
下面的示例演示如何將工作表移動到工作簿中的另一個位置。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); string InputPath = dataDir + "book1.xls"; // Open an existing excel file. Workbook wb = new Workbook(InputPath); // Create a Worksheets object with reference to // the sheets of the Workbook. WorksheetCollection sheets = wb.Worksheets; // Get the first worksheet. Worksheet worksheet = sheets[0]; // Move the first sheet to the third position in the workbook. worksheet.MoveTo(2); // Save the excel file. wb.Save(dataDir + "MoveWorksheet_out.xls");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn