I am creating an excel using Apache POI class. I have a column with data as 07898.
When I opened the excel the data is showing properly with 07898 with a warning message. When I click the mouse inside the excel is considering it as number and removing the 0 automatically.
But if I change the column type as text in Microsoft excel, it is not removing the 0. But I am not able to set the column type as Text. It is always Generic.
Any suggestion please let me know. Thank you.
I am setting the type as String, but it is not working properly.
SXSSFWorkbook hwb = new SXSSFWorkbook(100);
Sheet sheet = hwb.createSheet(strReportName);
Cell excelheadercell = excelheader.createCell((short) icolumns);
excelheadercell.setCellValue("07898");
CellStyle cellstyle = hwb.createCellStyle();
Font font = hwb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
excelheadercell.setCellStyle(cellstyle);
**excelheadercell.setCellType(Cell.CELL_TYPE_STRING);**
Related
I am trying to parse an excel file which is having some columns which are in Custom format.I have to convert them into Text before parsing.I am trying to implement a condition that if the celltype is blank and custom it will be converted to text format ,
I am trying this code but not working
Workbook workbook = excelManager.getWorkbook();
DataFormat fmt = workbook.createDataFormat();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(fmt.getFormat("#"));
But I think this # will not work for all custom cells. Can anyone please help me? I am using poi 3.16 and version can not be changed.
I want to lock an Excel cell (none-editable cell) using Apache POI. I'm using HSSFWorkbook and here what I've tried so far.
First I created a style.
Font mystyle = workbook.createFont();
headerFont.setFontHeightInPoints((short) 11);
style = workbook.createCellStyle();
style.setLocked(true);
Then I added it like below.
resultCell.setCellValue("mystyle");
resultCell.setCellStyle((CellStyle) formatter.styles.get("mystyle"));
But it doesn't lock the cell, still I can edit the cell. Is there any other way to achieve this ?
I have some code like :
CellStyle cs2 = wb.createCellStyle();
CellStyle cs4 = wb.createCellStyle();
cs4.setDataFormat(HSSFDataFormat.getBuiltinFormat("CELL_TYPE_NUMERIC"));
cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
this is for creating xls reports. How do i change this code to create XLSX reports ?
will the following work ?
XSSFDataFormat format = (XSSFDataFormat) wb.createDataFormat();
cs2.setDataFormat(format.getFormat("text"));
Please help.
Thanks
Yes, built-in formats will still work with .xlsx spreadsheets in Apache POI.
Even if the Javadocs for XSSFDataFormat#getFormat(String) don't mention it, the source code tells all:
public short getFormat(String format) {
int idx = BuiltinFormats.getBuiltinFormat(format);
if(idx == -1) idx = stylesSource.putNumberFormat(format);
return (short)idx;
}
It will look up the data format in the BuiltinFormats object first, and if not found, it will create a new one.
I've tested this on creating a .xlsx spreadsheet, and using "text" works. I created numeric cells, and I set those cells to a CellStyle with a XSSFDataFormat created with "text", and the cells are "text"-styled in the resultant spreadsheet.
I have a List of integers, that i want to export to Excel. But everytime the value is shown with decimal positions:
worksheet.getRow(0).getCell(1).setCellType(Cell.CELL_TYPE_NUMERIC);
worksheet.getRow(0).getCell(1).setCellValue(2); // Excel shows here 2.00
After that I've tried it with an custom style:
CellStyle numericStyle = workbook.createCellStyle();
numericStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#"));
worksheet.getRow(0).getCell(1).setCellValue(Double.parseDouble(2));
worksheet.getRow(0).getCell(1).setCellStyle(numericStyle);
Now it seem to be correct formated in Excel, but Excel (2013) says, the file is unsafe and should not be edited.
How is the correct way to format an integer-cell with poi?
Thanks for your help.
Try with:
// at the beggining, create one style that would be applied for all Integers
CellStyle integerStyle = workbook.createCellStyle();
integerStyle.setDataFormat(0);
// use style wherever you need it
worksheet.getRow(0).getCell(1).setCellValue(Integer.valueOf(2));
worksheet.getRow(0).getCell(1).setCellStyle(integerStyle);
numericStyle.setDataFormat(workbook.createDataFormat().getFormat("#,##0"));
where workbook is your HSSFWorkbook
Am using poi3.7 to create xlsx, its looking fine with openoffice but bold font not support in entire ms excel 2007 and 2010. its partially looking bold
style = objWorkbook.createCellStyle();
font = objWorkbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
objHSSFCell.setCellValue(new HSSFRichTextString(value));
objHSSFCell.setCellStyle(style);
pls give your suggestion to resolve this, thanks in advance.
Upgrading comment to an answer;
Seems you are using HSSF instead of XSSF for xlsx. Hence, though HSSFRichTextString is valid for an HSSFCell, you need XSSFCell instead and then set its value with XSSFRichTextString.