I am currently doing a program in Java that will run several tests and generate a report right after in excel. I was able to read and write through excel and the results of Passed or Failed are displayed in Results column. I was able to write these in excel but by supplying a default value on the cell (e.g. default) so the code will just overwrite it. I would like to write a comment on the Comment column, but I do not know how to write in a null cell. Here is a screenshot of the report I am generating (the link of the image available) and the code for reading and writing in excel as well.
FileInputStream fileInputStream = new FileInputStream("ReportExcel.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheetAt(0);
fileInput csvInputFile = new fileInput();
String[] sReturnValue = csvInputFile.arrayReturnSingleValue("fileInput.csv");
if (prodnameresult.equals(prodname) ){
Pass++;
totalResult++;
System.out.println ("Testcase1: Branding-Customised Product Name is PASSED");
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Passed");
}
else{
Fail++;
totalResult++;
System.out.println("Testcase1: Branding-Customised Product Name is FAILED");
//assertEquals(prodnameresult.equals(prodname), true);
//Harold's Input
HSSFRow row = worksheet.getRow(1);
HSSFCell cell = row.getCell((short) 4);
cell.setCellValue("Failed");
}
//Harold's Input
FileOutputStream os = new FileOutputStream("ReportExcel.xls");
workbook.write(os);
os.close();
}
Report:
http://i47.tinypic.com/2s1lsfc.png
I don't have access to the report, but to answer to your question, if your cell is null you should create it and not get it as following:
HSSFCell cell = row.getCell((short) 4);
if(cell == null)
cell = row.createCell((short) 4);
cell.setCellValue("Passed");
You should maybe re-apply the Style to the Cell if it had a different one from the row.
Related
I have a list of variables in an excel file which I use as input for an online app and generate a result. That occurs successfully however when I try to save the output in that same file by adding a new column and cells, the original content of the file would be deleted. I only want to add the info to the same document but the only option I found by googling is to create another file.
just to clarify:Variables for input
and instead of just adding the info this happens Changed document.
How can I fix it without adding more parameter and re-adding the info?
#Keyword
public void demoKey(String name) throws IOException{
FileInputStream fis = new FileInputStream("C://Users/i2srsm/Desktop/New Microsoft Excel Worksheet.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet("Data for full set");
int columnNumber = sheet.getRow(0).getLastCellNum();
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
sheet.createRow(firstRow).createCell(columnNumber).setCellValue('Proposta');
for (int rn=(firstRow); rn<=lastRow; rn++){
Cell cell = sheet.createRow(rn).createCell(columnNumber+1)
cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue(name);
FileOutputStream fos = new FileOutputStream("C://Users/i2srsm/Desktop/New Microsoft Excel Worksheet.xlsx");
workbook.write(fos);
fos.close();
}
}
}
don't use a sheet.createRow(row index) for update the existing excel file. this one create a new row. if you want to update the existing row in a sheet, firstly get related existing row and then create a new cell.
for get existing row
Row row = sheet.getRow(row index);
for create a new cell in above existing row
Cell cell = row.createCell(cell index);
try with this
sheet.getRow(firstRow).createCell(columnNumber).setCellValue("Proposta");
for (int i=(firstRow+1); i<=lastRow; i++){
Row row = sheet.getRow(i);
Cell cell = row.createCell(columnNumber);
cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue(name);
}
FileOutputStream fos = new FileOutputStream("C:/Users/LifeStyle/Documents/output.xlsx");
workbook.write(fos);
fos.close();
I have an app that imports an excel but the excel in question has black spaces in some of the cells, when I receive it puts those blank spaces in my database. I want to prevent that to happen, i tried to use regex and replace(" ","") and nothing work. Can you help me? This is my code:
File file = new File(inFileName);
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
int rowCtr = 34;
Row myRow = sheet.getRow(rowCtr++);
while (myRow != null) {
Cell nif = myRow.getCell(0);
Cell marcaexploracao = myRow.getCell(1);
Cell numerochip = myRow.getCell(2);
Cell marcaauricular = myRow.getCell(3);
Cell datanascimento = myRow.getCell(4);
//problem I mentioned is is in chipnumber only
bd.addAnimais(chipnumber.ToString().replaceFirst("/[^0-9]/","-"),marcaexploracao.toString(),marcaauricular.toString(),datanascimento.toString(),nif.toString(),0,"","","","",0);
myRow = sheet.getRow(rowCtr++);
}
}catch (Exception e){e.printStackTrace(); }
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();
This question already has answers here:
Multiline text in Excel cells
(3 answers)
Closed 7 years ago.
I have got a problem to create new lines into a cell. When I create a file, the new lines aren't shown in correct way. I hope someone can help me to solve this issue.
cell.setCellValue(text);
if(text.indexOf(System.getProperty("line.separator")) != -1)
{
int countRows = text.split(System.getProperty("line.separator")).length;
style.setWrapText(true);
cell.setCellStyle(style);
row.setHeightInPoints((countRows * sheet.getDefaultRowHeightInPoints()));
sheet.autoSizeColumn((short) countRows);
}
This is my Excel-File
When I make a new Excel-File, all the cells are looking like cell A1. There isn't a new line. When I click on the cell A1 shows the description over the cell the content with the new lines.
I made a double click in cell A2 and the cell is showing the content in the correct way.
The new line tag seems to work. My issue is, that the new line isn't shown at the cell in the excel file. But it is shown at the cell description. Is there a way to show all of the cells like the cell A2 without a double click?
Thanks
\n should be \r\n for excel files.
This worked for me ,
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short) 0);
row.setHeight((short) (2*sheet.getDefaultRowHeight()));
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
Cell cell = row.createCell(0);
cell.setCellStyle(cs);
cell.setCellValue(
createHelper.createRichTextString("This is \n a string"));
wb.write(fileOut);
fileOut.close();
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.