I want to write numbers to Excel in scientific notation. In Apache Poi it is possible to set the number format of a cell like this:
Cell cell = ...
CellStyle style = workbook.createCellStyle();
DataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat("#0.00"));
cell.setCellStyle(style);
But when I use a pattern like "#0.00E0" I get an error from Excel when opening the file and the number format is lost.
Here is a full example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Main {
public static void main(String[] args) {
try (Workbook wb = new XSSFWorkbook();
FileOutputStream fos = new FileOutputStream("out.xlsx")) {
Sheet sheet = wb.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
CellStyle style = wb.createCellStyle();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("#0.00E0"));
cell.setCellStyle(style);
cell.setCellValue(Math.random());
wb.write(fos);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Thanks!
The correct format for scientific notation would be:
format.getFormat("0.00E+00")
Related
i need to change the author name of the generated excel by apache poi in java. Currently the author name of all generated by apache is "Apache POI", i need to change it. can anyone help me on this?
Thanks in advanced.Generated by Apache poi
HSSFWorkbook is a POIDocument which has SummaryInformation.
XSSFWorkbook is a POIXMLDocument which has POIXMLProperties - POIXMLProperties.CoreProperties.
Code to set author (aka creator) for both XSSF and HSSF:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
class CreateExcelAuthor {
public static void main(String[] args) throws Exception {
String author = "Axel Richter";
Workbook workbook = new XSSFWorkbook();
//Workbook workbook = new HSSFWorkbook();
workbook.createSheet();
if (workbook instanceof XSSFWorkbook) {
((XSSFWorkbook)workbook).getProperties().getCoreProperties().setCreator(author);
} else if (workbook instanceof HSSFWorkbook) {
((HSSFWorkbook)workbook).createInformationProperties();
((HSSFWorkbook)workbook).getSummaryInformation().setAuthor(author);
}
String fileName = (workbook instanceof XSSFWorkbook)?"Excel.xlsx":"Excel.xls";
try (FileOutputStream out = new FileOutputStream(fileName) ) {
workbook.write(out);
}
workbook.close();
}
}
I'd created excel report(.xls) using Apache POI. Whenever I open it, it's showing a message "PROTECTED VIEW: Office has detected a problem with this file. Editing it may harm your computer". How to disable the protected view and Can I handle this within the code itself?
I'm getting the error message only when I apply the style to the cell.
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class Test {
public static void main(String[] args) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = null;
HSSFDataFormat dtFormat = wb.createDataFormat();
HSSFRow row_data = null;
HSSFCell cell_data = null;
HSSFCellStyle style = null;
HSSFCellStyle styleSubHeader = null;
HSSFCellStyle styleLeft = null;
HSSFCellStyle styleCentre = null;
HSSFCellStyle styleBRData = null;
HSSFCellStyle styleRight = null;
HSSFCellStyle styleRPrec = null;
HSSFCellStyle styleBold = null;
HSSFCellStyle styleBRight = null;
HSSFCellStyle styleBRPrec = null;
HSSFCellStyle styleBLeftHead = null;
HSSFCellStyle styleBLeft = null;
HSSFCellStyle styleBCentre = null;
HSSFCellStyle styleRRight = null;
HSSFCellStyle styleSubTitle = null;
HSSFFont headBold = null;
HSSFFont titleBold = null;
HSSFDataFormat dtFmt = null;
HSSFCellStyle styleCenter = wb.createCellStyle();
HSSFFont fontCenter = wb.createFont();
HSSFFont font = wb.createFont();
HSSFFont fontBold = wb.createFont();
HSSFFont fontsubTitle = wb.createFont();
FileOutputStream out = new FileOutputStream(new File("sample.xls"));
try {
styleSubTitle = wb.createCellStyle();
sheet = wb.createSheet("Pricing Report");
row_data = sheet.createRow(sheet.getLastRowNum());
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFColor.TAN.index);
style.setBorderLeft(HSSFColor.TAN.index);
style.setBorderRight(HSSFColor.TAN.index);
style.setBorderTop(HSSFColor.TAN.index);
cell_data = row_data.createCell((short) 0);
cell_data.setCellValue("Header 1");
cell_data.setCellStyle(styleSubTitle);
cell_data.setCellStyle(style);
wb.write(out);
out.close();
System.out.println("Excel Generated");
} catch (Exception e) {
e.printStackTrace();
}
}
}
So you are just another user of ancient versions of apache poi. I would suggest you using the last stable version 3.17 instead of that 6 years old 3.9.
So for all who will find this later: This code is using apache poi version 3.10 or lower and will not more work in current versions.
And what do you expect the style.setBorderBottom(HSSFColor.TAN.index); will do? The setBorderBottom does setting the thickness of the border. This should be style.setBorderBottom(HSSFCellStyle.BORDER_THICK); for example in your version. The setting the border color will be style.setBottomBorderColor(HSSFColor.TAN.index);.
This is the problem. The int HSSFColor.TAN.index is 0x2f and this is simply not allowed to be a border thickness. That's why Excel denies using the file as a safe Excel file.
So:
...
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBottomBorderColor(HSSFColor.TAN.index);
style.setLeftBorderColor(HSSFColor.TAN.index);
style.setRightBorderColor(HSSFColor.TAN.index);
style.setTopBorderColor(HSSFColor.TAN.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THICK);
style.setBorderLeft(HSSFCellStyle.BORDER_THICK);
style.setBorderRight(HSSFCellStyle.BORDER_THICK);
style.setBorderTop(HSSFCellStyle.BORDER_THICK);
...
I have downloaded Apache Poi Jar but when I write the following code (which a youtube instructor ran with ease), it does not give me any output excel file. What am I doing wrong here? I hover my mouse over HSSFWorkbook eclipse tells me
org.apache.poi.hssf.usermodel.HSSFWorkbook Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
The code.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class WriteExcel {
public static void main(String[] args) throws FileNotFoundException, IOException {
HSSFWorkbook workbook= new HSSFWorkbook();
HSSFSheet sheet= workbook.createSheet("FirstExcelSheet");
HSSFRow row= sheet.createRow(0);
HSSFCell cell= row.createCell(0);
cell.setCellValue("1,Cell");
workbook.write(new FileOutputStream("excel.xls"));
workbook.close();
workbook.getFirstVisibleTab();
}
}
the first thing use XSSFWorkbook instead of HSSFWorkbook take a look at this link,
and the second thing you have to out stream this file add this line
try (FileOutputStream outputStream = new FileOutputStream("slsx.xlsx")) {
workbook.write(outputStream);
so your code should be like this
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("sheet1");
XSSFRow row = spreadsheet.createRow(1);
XSSFCell cell = row.createCell(1);
cell.setCellValue("test");
try (FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"))) {
workbook.write(out);
}
System.out.println("Writesheet.xlsx written successfully");
}
you should add your path in this line like:
workbook.write(new FileOutputStream("{yourpath}/excel.xls"));
it works..
I wrote a code in Java to read an excel(.xlsx) file and save the same file with different name. The code is working fine but the size of original and generated excel is different. I got the problem as on saving the original, the excel is losing some data. But when I opened the generated file through Ms Excel it get restored to its original size. What could I use in my code so that no data is lost?
Code is:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class MyExcel {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("D:\\test.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(1);
XSSFRow row1 = sheet.getRow(1);
XSSFCell cell1 = row1.getCell(1);
cell1.setCellValue("Mahesh");
XSSFRow row2 = sheet.getRow(2);
XSSFCell cell2 = row2.getCell(1);
cell2.setCellValue("Ramesh");
OutputStream fos = new FileOutputStream(new File("D:\\test_new.xlsx"));
workbook.write(fos);
fis.close();
fos.close();
System.out.println("Done");
}
}
The image shows the different in size of two files.
I want to add a cell in xlsx workbooks sheet containing the quote prefix, and i am trying to create that sheet using POI library. How do I add this type of cell
I found a reference to this with CTXf.setQuotePrefix(boolean quotePrefix) on maven central, but dont know how to add this to the XSSFCell
I have tried using below code
XSSFCell cell=row.createCell(cellIndex);
CTXfImpl ctxf= new CTXfImpl(XmlObject.Factory.newInstance().schemaType());
ctxf.setQuotePrefix(true);
cell.getCTCell().set(ctxf);
cell.setCellValue(data);
getting exception
Exception in thread "main" java.lang.NullPointerException
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTXfImpl.setQuotePrefix(Unknown Source)
Can anyone help me with this
The CTXf and also the quotePrefix property is part of the XSSFCellStyle and not the XSSFCell.
So we must create a XSSFCellStyle, set the quotePrefix there and then apply this XSSFCellStyle to the XSSFCell.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
class WriteQuotePrefix {
public static void main(String[] args) {
try {
Workbook wb = new XSSFWorkbook();
CellStyle style = wb.createCellStyle();
((XSSFCellStyle)style).getCoreXf().setQuotePrefix(true);
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue("1234");
FileOutputStream fileOut = new FileOutputStream("WriteQuotePrefix.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (IOException ioex) {
}
}
}