I am trying to write data to an excel sheet using JExcel. I have an excel file placed on disk and I opened it using JExcel and then created a Writable Workbook.
The Excel workbook contains sheets having pivot table. Idea is to just populate source sheets and then refresh the pivot tables. But after creating Writable Workbook, the formatting and pivot tables got away.
Here is my code snippet:
FileInputStream fileInputStream = new FileInputStream("C:\\Pivot_File_template.xls");
workbook = Workbook.getWorkbook(fileInputStream);
WorkbookSettings workbookSettings = new WorkbookSettings();
writableWorkbook = Workbook.createWorkbook(new File("C:\\Writable_Pivot_File.xls"),workbook,workbookSettings);
The file "Writable_Pivot_File.xls" just lost the orignal formatting and pivot tables. Any suggestions what I can do to maintain all the formatting and pivot tables? I am using "jxl_2.6.9.jar". Thanks in advance.
Just got away with this issue by using Apache POI. JExcel doesnot support xlsx and updating an xls file having pivot table using JExcel will disable the Pivot table and change its formatting.
Related
I use apache poi. I want to get object of Workbook class using following code.
Unfortunately WorkbookFactory tries to read pivot tables cache that causes error. Is it possible to disable reading pivot tables? How to do this in apache poi. Thank you for help in advance.
try (Workbook workbook = WorkbookFactory.create(inputFile.getInputStream())) { ... }
I have one excel template placed in my application's configuration folder. Now while downloading that excel template, I am trying to edit it's data and existing formulas using POI api. I am able to edit data easily.
How can I edit existing formulas? The formula which I am trying to edit, is attached with more than 1000 cells, so instead editing every cell and updating formula, I am trying to update that formula itself.
I am using following code to get workbook object
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("ExcelTemplate.xlsx")));
XSSFSheet sheet = workbook.getSheetAt(5);
for(int i = 2; i<174; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(0);
cell.setCellValue("My Data");
}
FileOutputStream output_file =new FileOutputStream(new File("UpdatedExcelTemplate.xlsx"));
workbook.write(output_file);
output_file.close();
Problem which I am facing is as following:
I have one sheet (say sheetA) in which I am inserting values at run time. And I am referring that list of data in another sheet (say sheetB) as drop down for any cells. Now as per data range in sheetA, my formula should get change.
I have an .xlsx file with multiple sheets containing different data. Of all the sheets one sheet needs to accommodate close to 100,000 rows of data, and the data needs to be written using Java with poi.
This seems quite fast and simple with SXSSFWorkbook, where I can keep only 100 rows in memory, but the disadvantage is that I can only write to a new file (or overwrite existing file).
Also, I am not allowed to 'load' an existing file, i.e
SXSSFWorkbook wb = new SXSSFWorkbook(file_input_stream) is not allowed.
I can use Workbook factory:
Workbook workbook = new SXSSFWorkbook();
workbook = WorkbookFactory.create(file_input_stream);
but when the time comes for me to flush the rows,
((SXSSFSheet)sheet).flushRows(100);
I get the error that type conversion is not allowed from XSSFSheet to SXSSFSheet.
I tried to see if there was any way to copy sheets across different workbooks, but so far it seems it has to be done cell by cell.
Any insights on how to approach this problem?
You are probably having a template to which you want to add large data. You need to use the SXSSFWorkbook(XSSFWorkbook) constructor:
XSSFWorkbook wb = new XSSFWorkbook(new File("template.xlsx"));
SXSSFWorkbook wbss = new SXSSFWorkbook(wb, 100);
Sheet sheet = wbss.createSheet("sheet1");
// now add rows to sheet
I'm using workbook.removeSheetAt() to remove a certain named sheet from a workbook. However, the workbook contains a new sheet called "Sheet3" after writing it out to disk. What gives? I'm not creating this new sheet anywhere in my code. Note: I am using XSSFWorkbook and all the other XSSF stuff.
Some more info: Here is a snippet from workbook.xml inside the output xlsx. I am deleting sheet with index=2, ostensibly sheetId=3. The template workbook I am starting with only has four sheets.
<sheets>
<sheet r:id="rId1" sheetId="1" name="Sheet1"/>
<sheet r:id="rId2" sheetId="2" name="Params" state="hidden"/>
<sheet r:id="rId4" sheetId="4" name="Warnings" state="hidden"/>
<sheet r:id="rId3" sheetId="5" name="Sheet3"/>
</sheets>
Apache POI is using index based sheets (kinda like an array). If you have a sheet at a position that is greater than x, there must exist a sheet at position x even if it is blank. You could try moving the sheets where n > x to position n - 1 using setSheetOrder
Seems like there is a bug report similar to this: Bug Report
What about hiding the sheet: setSheetHidden
This question already has answers here:
Copying Excel Worksheets in POI
(6 answers)
Closed 3 years ago.
I have one excel file with single sheet (abstract model). Now I want to copy the sheet to another existing workbook. How can I do this?
For processing regular styles and data we could iterate through each cells.
But if we have formula enabled cell,non editable cells, merged cell then this is the better solution to go for:
I have tried something like copying sheets but it didn't worked.
In addition with that i need to copy a sheet in which there are some non editable cells and formula enabled cells too.
This solution worked for me:
1)copy the entire workbook
2)delete unnecessary sheets
3)add your new sheets to above book
//input source excel file which contains sheets to be copied
file = new FileInputStream("C:\\SamepleTemplate.xlsx");
workbookinput = new XSSFWorkbook(file);
//output new excel file to which we need to copy the above sheets
//this would copy entire workbook from source
XSSFWorkbook workbookoutput=workbookinput;
//delete one or two unnecessary sheets, you can delete them by specifying the sheetnames
workbook.removeSheetAt(workbook.getSheetIndex(workbook.getSheet(" your sheet name ")));
//if you want to delete more sheets you can use a for to delete the sheets
for (int index=0;index<=5;index++)
{
workbook.removeSheetAt(index);
}
//To write your changes to new workbook
FileOutputStream out = new FileOutputStream("C:\\FinalOutput.xlsx");
workbookoutput.write(out);
out.close();