HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
HSSFRow hssfRow = hssfSheet.createRow(0);
HSSFCell hssfCell = hssfRow.createCell(i);
hssfCell.setCellValue(name.getText().toString());
i want to store previous hssf cell value in excel , when i enter new cell value it does,nt store previous hssf cell value...
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);
}
Hi I am trying to get the values of two columns with the column name. I have uploaded an excel sheet and I have to fetch the values of this two columns and then save it in DB. Can someone help me with this
Include the latest POI jar
FileInputStream inputStream = ew FileInputStream(" Excel Path ");
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet workSheet = workbook.getSheet(0); // put your sheet number
Iterator<Row> rowIterator = workSheet.iterator();
while (rowIterator.hasNext()) {
Row nextRow = rowIterator.next();
Cell cell = nextRow.getCell("col name");
String cellData = cell.getStringCellValue();}
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 wanted to set font of the title row in a spreadsheet bold. I was able to do that in my main function with the following code:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet dataSheet = workbook.createSheet("Data");
HSSFCellStyle fontStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBold(true);
fontStyle.setFont(font);
Row row = dataSheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(fontStyle);
cell.setCellValue("ID");
Since createCellStyle is method of HSSFWorkbook, if I write to the sheet by calling a function which takes the sheet but not the workbook as parameter, how do I set the cell style?
public class SummaryXlsCreator {
public static void main(String[] args) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet dataSheet = workbook.createSheet("Data");
writeCDMarker(dataSheet);
}
public static void writeCDMarker(HSSFSheet sheet) {
int rownum = 0;
int cellnum = 0;
Row row = sheet.createRow(rownum++);
Cell cell = row.createCell(cellnum++);
// write first row of sheet "Data"
cell.setCellValue("ID");
}
Use getWorkbook() to get the parent and proceed with the existing code.
sheet.getWorkbbok().createCellStyle();
https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html
You can use the static CellUtil.setCellStyleProperties(Cell cell, Map<String, Object> properties) to format your cell without needing a reference to your workbook or sheet.
Example usage:
// Format cell into date time format with "m/d/yy h:mm",
// which is converted to user's locale in Excel.
CellUtil.setCellStyleProperties(cell, Collections.singletonMap(CellUtil.DATA_FORMAT, (short) 22));
I have come across need to add table or rows inside cell in excel.
I googled but did't found any thing.
Please help me know how to do it.
are you looking for this ?
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((short)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
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();