文檔首頁(yè)>>Spire.XLS 系列教程>>Spire.XLS教程:在 Java 中將工作表從一個(gè)工作簿復(fù)制到另一個(gè)工作簿
Spire.XLS教程:在 Java 中將工作表從一個(gè)工作簿復(fù)制到另一個(gè)工作簿
Spire.XLS for Java是專業(yè)的Java Excel API,使開發(fā)人員無(wú)需使用Microsoft Office或Microsoft Excel即可創(chuàng)建,管理,操作,轉(zhuǎn)換和打印Excel工作表。
本文展示了如何通過(guò)使用Spire.XLS for Java將一個(gè)工作表從一個(gè)Excel文檔復(fù)制到另一個(gè)。
代碼如下:
import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class CopySheetBetweenWorkbooks { public static void main(String[] args) { //Create a Workbook object to load the source document Workbook srcWorkbook = new Workbook(); srcWorkbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.xlsx"); //Get the specific worksheet to copy Worksheet originalSheet = srcWorkbook.getWorksheets().get(0); //Create another Workbook object to load the destination document Workbook destWorkbook = new Workbook(); destWorkbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\destination.xlsx"); //Add the copy of selected sheet to destination document Worksheet newSheet = destWorkbook.getWorksheets().addCopy(originalSheet); newSheet.setName(originalSheet.getName()); //Copy the theme of source document to destination document destWorkbook.copyTheme(srcWorkbook); //Save to another file destWorkbook.saveToFile("CopySheetBetweenWorkbooks.xlsx"); } }