I'm developing an app in java using POI, which writes data into excell sheet.
I want to write a new row when I have a new data (in order to assist the user to follow the data).
I dont want to close and reopen the excell file each time I have a new data to write.
The initilize code is:
FileOutputStream fileOut = new FileOutputStream(new File (excellFileName));
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
rowNum = 0;
HSSFRow rowhead = sheet.createRow((short)rowNum);
createRowColumns(rowhead, rowNum);
workbook.write(fileOut);
rowNum++;
Each time I have a new data, I'm using this code:
HSSFRow rowMsg = sheet.createRow((short)rowNum);
createRowColumns(rowMsg, rowNum);
workbook.write(fileOut);
rowNum++;
(createRowColumns mehtod sets the data (in seperate cells in a new: rowMsg)
The problem is that I cant see any rows in the excell file, except the first row (rowhead , row #0).
What Am I missing ?
(Pay attention that I dont want to close and reopen the file each time I have data to write)
Thanks
To write all rows you have to write to worksheet only once. So, you need to update your code as follows.
for(int index = 0; index < rowNum; rowNum++) {
HSSFRow rowMsg = sheet.createRow((short)rowNum);
createRowColumns(rowMsg, rowNum);
}
workbook.write(fileOut);
Related
List having data in this format
[{Row ID=7565.0, Product ID=test11223, Postal Code=98103.0}, {Row ID=7567.0, Product ID=test11213, Postal Code=98101.0}] Having more than 100 row ID record like this, I want to store that data in Excel in below format
Row Id Order ID Postal Code
7565 test11233 98103
7567 test11233 98101
Please help to share the code to write a above list data in excel. thnx
With Apache POI you can write any kind of MS Office files.
Here is a quick example how write cells with it.
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
// Write the output to a file
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}
I have been testing an application where i have to write a set of results to the excel file, the values are taken from the script. But the problem is that first I have to set the column headings like "Sl No" and "Name" after that I have to print the values against the particular cells. The thing is that am not able to write the results under the particular box.
I am new to apache poi. Can anyone help me with this issue.
Code for setting the excel and headings are shown below,
String FileName = "C://Users//Desktop//filename.xlsx";
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("Distributor");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Sl No");
Cell cell2 = row.createCell(1);
cell2.setCellValue("Name");
FileOutputStream outputStream = new FileOutputStream(new File(FileName));
workBook.write(outputStream);
outputStream.close();
When I add data to second tab in excel sheet. First tab data is not displaying.
If i remove code for second tab, first tab data is visible.
HSSFSheet sheet1 = wb.createSheet("A ");
HSSFSheet sheet2 = wb.createSheet("B");
HSSFFont headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
headerStyle.setFillBackgroundColor(HSSFColor.WHITE.index);
headerStyle.setFont(headerFont);
try
{
FileOutputStream fileOut = new FileOutputStream("C:\\Report.xls");
HSSFRow sessionname = sheet1.createRow(0);
HSSFCell title = sessionname.createCell(0);
title.setCellStyle(headerStyle);
title.setCellValue("Supported cipher Report");
HSSFRow row = sheet1.createRow(5);
HSSFCell cell0 = row.createCell(0);
cell0.setCellStyle(headerStyle);
cell0.setCellValue("One");
HSSFCell cell1 = row.createCell(1);
cell1.setCellStyle(headerStyle);
cell1.setCellValue("two");
HSSFCell cell2 = row.createCell(2);
cell2.setCellStyle(headerStyle);
cell2.setCellValue("three");
HSSFCell cell3 = row.createCell(3);
cell3.setCellStyle(headerStyle);
cell3.setCellValue("four");
HSSFCell cell4 = row.createCell(4);
cell4.setCellStyle(headerStyle);
cell4.setCellValue("five");
if (!list.isEmpty())
{
int rowNumber = 6;
for (Result s : supportedlist)
{
HSSFRow nextrow = sheet1.createRow(rowNumber);
nextrow.createCell(0).setCellValue(s.One());
nextrow.createCell(1).setCellValue(s.Two());
nextrow.createCell(2).setCellValue(s.Three());
nextrow.createCell(3).setCellValue(s.four());
nextrow.createCell(4).setCellValue(s.five());
rowNumber++;
}
}
sheet1.autoSizeColumn(0);
sheet1.autoSizeColumn(1);
sheet1.autoSizeColumn(2);
sheet1.autoSizeColumn(3);
sheet1.autoSizeColumn(4);
System.out.println("sheet1 writting");
wb.write(fileOut);
fileOut.flush();
fileOut.close();
when i pass sheet2 and remaining code is same as above.
sheet1 tab data is not displayed while sheet2 data is desplayed.
I think what you are doing is you are changing the values of sheet1 to sheet2 after writing to sheet1. Now, what happens according to your code is you are again creating/writing to a file. So, it replaces the old file and data and writes to a new file in second sheet.
Below line overwrites the data when you run the program second time
FileOutputStream fileOut = new FileOutputStream("C:\\Report.xls");
What you should do is when writing to sheet2 you should check if file exists or not. If exists then it adds new sheet to existing excel file. If not exists create a new file and then create a sheet.
Do something like this
FileOutputStream fileOut = new FileOutputStream(file);
if (file.exists()) {
try {
workbook = (HSSFWorkbook)WorkbookFactory.create(file);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
HSSFSheet sheet = workbook.createSheet("Sample sheet2");
}
else{
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet1");
}
workbook.write(fileOut);
fileOut.close();
To avoid overwriting to a existing file use below. This will append the data to the existing file.
FileOutputStream fileOut = new FileOutputStream("C:\\Report.xls", true);
problem was I created two instances[new createExcel(one); and new createExcel(two); while invoking the function. since wb is part of instance. so it created new excel with sheet1 and sheet2 inside. and since this time I write sheet2 data only. so sheet2 data was only displaying. Thanks for the support.
I am reading an already existing excel file and trying to add certain cells for example(C4-C15). I am having difficult manipulating the files through java. I am using apache poi, and would appreciate any help or direction.
In order to access a cell in Apache POI, you have to get a row first, and then get cell within chosen row. Below is example:
//Input file
InputStream inp = new FileInputStream("workbook.xls");
//Create workbook instance
Workbook wb = WorkbookFactory.create(inp);
//Create sheet instance
Sheet sheet = wb.getSheetAt(0);
for(int i = 3; i <= 16; ++i){ //Rows from 4 to 15 (Apache POI is zero based)
Row row = sheet.getRow(i);
Cell cell = row.getCell(2); //Column "C"
//Do something with cell
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Source (more examples here)
I have a java program that prints 1000 integer values each I run it. I want to copy the output to an excel file each time I run the program. I want to output the first run in the first column in excel file and then copy the next runs in the subsequent columns in the same excel file. For example:
Run: 1
value1
value2
.
.
value1000
Run:2
value1
value2
.
.
value1000
I want the first output in the first column of an excel file and the second output in the second column
Here is my code:
int rownum=1;
int cellnum=1;
File file;
HSSFWorkbook workbook;
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
public void writeOutput(double meandispersion) {
String dispersion = Double.toString(meandispersion);
HSSFRow row = sheet.createRow(rownum++);
HSSFCell cell = row.createCell(cellnum);
cell.setCellValue("dispersion");
try {
FileOutputStream out = new FileOutputStream("newFile.xls");
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
There are a total of 1000 time steps in the main code and in each time step this method is called and the value of meandispersion is passed to it. It prints the 1000 values in 1000 rows in the first column. The problem is that when I run the program second time I want to copy the 1000 values in the second column, for third run 3rd column and so on. Currently it is not appending the values, it overwrites the entire file. Can anyone point out the problem?
You have to read the existing file, append your new output to the existing data yourself (in correct column and then write it to the file.
Right now you are not reading the file at all, which would overwrite the existing contents.
Hope this helps.
POI's Quick Guide aka the "Busy Developers' Guide to HSSF and XSSF Features" contains lots of code snippets, including one that talks about opening an existing workbook, and reading and writing and saving the modified workbook (example verbatim copied from that page, added some comments):
InputStream inp = new FileInputStream("workbook.xls");
// notice how the Workbook must be constructed from the existing file
Workbook wb = WorkbookFactory.create(inp);
// Navigating in POI always follows the same logic:
// 1. grab a sheet
// 2. grab a row from that sheet
// 3. grab a cell from that row
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
// a condition like the one that follows will be needed to know in what column
// you have to write your data:
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
That, and the other examples on that page should get you up to speed quickly.