翻譯|行業(yè)資訊|編輯:李顯亮|2021-03-16 10:01:10.193|閱讀 351 次
概述:在某些情況下,可能需要將工作表復(fù)制到Excel工作簿中,為了以編程方式執(zhí)行上述任務(wù),本文介紹了如何使用C#在Excel工作簿中復(fù)制工作表。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
在某些情況下,可能需要將工作表復(fù)制到Excel工作簿中,或從一個(gè)工作簿復(fù)制到另一個(gè)工作簿。另一方面,在不同的情況下,也需要移動(dòng)工作表以更改其在工作簿中的位置。為了以編程方式執(zhí)行上述任務(wù),本文介紹了如何使用C#在Excel工作簿中復(fù)制工作表。此外,還介紹了如何更改Excel工作簿中工作表的位置。
為了在Excel文件中復(fù)制工作表,我們將利用Aspose.Cells for .Net,該API為您提供了廣泛的功能,可以從.NET應(yīng)用程序中生成和處理Excel文件。點(diǎn)擊下方按鈕可下載試用。
以下是使用Aspose.Cells for .NET在Excel工作簿中復(fù)制工作表的步驟。
下面的代碼示例演示如何使用C#在Excel工作簿中復(fù)制工作表。
// Open an existing Excel file Workbook wb = new Workbook("workbook.xlsx"); // Create a WorksheetCollection 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("CopyWithinWorkbook.xlsx");
現(xiàn)在,讓我們看一下如何將工作表從一個(gè)Excel工作簿復(fù)制到另一個(gè)工作表。以下是執(zhí)行此操作的步驟。
下面的代碼示例演示如何使用C#將工作表從一個(gè)工作簿復(fù)制到另一個(gè)工作簿。
// Open source Excel file Workbook sourceWorkbook = new Workbook("source.xlsx"); // Open destination Excel file Workbook destinationWorkbook = new Workbook("destination.xlsx"); // Copy the first sheet of the source workbook into destination workbook destinationWorkbook.Worksheets[0].Copy(sourceWorkbook.Worksheets[0]); // Save the Excel file destinationWorkbook.Save("copy-worksheets.xlsx");
在某些情況下,可能需要重新排列工作簿中工作表的位置。可以通過(guò)指定索引將工作表移動(dòng)到所需位置來(lái)完成此操作。以下是將工作表從一個(gè)位置移動(dòng)到另一位置的步驟。
下面的代碼示例演示如何使用C#將Excel工作表從一個(gè)位置移動(dòng)到另一位置。
// Open an existing excel file Workbook wb = new Workbook("workbook.xlsx"); // Create a WorksheetCollection 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("move-worksheet.xlsx");
如果你想試用Aspose的全部完整功能,可 聯(lián)系在線(xiàn)客服獲取30天臨時(shí)授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn