Copying and pasting content within or between documents is one of the most common operations in our daily work. This article will introduce how to copy a cell range within a worksheet or between two worksheets in a single Excel document by using Free Spire.XLS for Java.
Installation (2 Method)
1# Download the Free Spire.XLS for Java and unzip it, then add the Spire.Xls.jar file to your project as dependency.
2# You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls.free</artifactId> <version>3.9.1</version> </dependency> </dependencies>
Copy a cell range within a worksheet
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class CopyRow { public static void main(String[] args) { //Create a Workbook instance Workbook wb = new Workbook(); //Load a sample Excel file wb.loadFromFile("C:\\Users\\Administrator\\Documents\\input.xlsx", ExcelVersion.Version2013); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Copy a cell range within a worksheet sheet.copy(sheet.getCellRange("A1:G1"),sheet.getCellRange("A16:G16"),true); //Save the document wb.saveToFile("CopyRangeWithinSheet.xlsx", ExcelVersion.Version2013); } }
Copy a cell range from one worksheet to another
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class CopyRow { public static void main(String[] args) { //Create a Workbook instance Workbook wb = new Workbook(); //Load a sample Excel file wb.loadFromFile("C:\\Users\\Administrator\\Documents\\input.xlsx", ExcelVersion.Version2013); //Get the first worksheet Worksheet sheet1 = wb.getWorksheets().get(0); //Get the second worksheet Worksheet sheet2 = wb.getWorksheets().get(1); //Copy a cell range from sheet 1 to sheet 2 sheet1.copy(sheet1.getCellRange("A1:G1"),sheet2.getCellRange("A1:G1"),true); //Save the document wb.saveToFile("CopyRangeBetweenSheets.xlsx", ExcelVersion.Version2013); } }
Top comments (0)