article

How to Copy Data from Range of Cells with Style in Workbook inside Java Apps

Email
Submitted on: 8/26/2015 11:02:51 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.4), Java (JDK 1.5)
Views: 4066
 
     This technical tip show how developers can Copy Range Data with Style in an Excel Workbook inside Java Applications. Copy Range Data Only explained how to copy the data from a range of cells to another range. Aspose.Cells can also copy a range complete with formatting. This article explains how to achieve this task. Aspose.Cells provides a range of classes and methods for working with ranges, for example createRange(), StyleFlag, applyStyle() etc.

 
				This technical tip show how developers can Copy Range Data with Style in an Excel Workbook inside Java Applications. Copy Range Data Only explained how to copy the data from a range of cells to another range. Aspose.Cells can also copy a range complete with formatting. This article explains how to achieve this task. Aspose.Cells provides a range of classes and methods for working with ranges, for example createRange(), StyleFlag, applyStyle() etc. This example explains how to:
•	Creates a workbook.
•	Fills a number of cells in the first worksheet with data.
•	Creates a range.
•	Creates a style object with specified formatting attributes.
•	Applies the style to the data range.
•	Creates a second range of cells.
•	Copies data with the formatting from the first range to the second range.
Copy Range Data with Style in a workbook
[Java]
 
String filePath = "F:/Downloads/source.xlsx";
//Load your source workbook
Workbook workbook = new Workbook(filePath);
//0-byte array
byte[] workbookData = new byte[0];
//Text save options. You can use any type of separator
TxtSaveOptions opts = new TxtSaveOptions();
opts.setSeparator('\t');
//Copy each worksheet data in text format inside workbook data array
for (int idx = 0; idx < workbook.getWorksheets().getCount(); idx++)
{
//Save the active worksheet into text format
ByteArrayOutputStream bout = new ByteArrayOutputStream();
workbook.getWorksheets().setActiveSheetIndex(idx);
workbook.save(bout, opts);
//Save the worksheet data into sheet data array
byte[] sheetData = bout.toByteArray();
//Combine this worksheet data into workbook data array
byte[] combinedArray = new byte[workbookData.length + sheetData.length];
System.arraycopy(workbookData, 0, combinedArray, 0, workbookData.length);
System.arraycopy(sheetData, 0, combinedArray, workbookData.length, sheetData.length);
workbookData = combinedArray;
}
//Save entire workbook data into file
FileOutputStream fout = new FileOutputStream(filePath + ".out.txt");
fout.write(workbookData);
fout.close();

More about Aspose.Cells for Java


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.