Domino Java extract attachment and read excel using Apache POI - java

I am trying to extract "excel template" from the existing notes document to the file system and reading using Apache POI. I am trying to extract excel template only if it doesn't already exist in the directory, else trying to read from the same location but it's throwing null exception when it runs second time.
here is my code,
//If it has an attachment
if (doc.hasEmbedded()) {
RichTextItem body = (RichTextItem)docTmpl.getFirstItem("Body");
Vector atts = body.getEmbeddedObjects();
EmbeddedObject att = (EmbeddedObject)atts.elementAt(0);
if ( att.getType() == EmbeddedObject.EMBED_ATTACHMENT ) {
String templatePath = "C:\\externalfiles\\excel\\" + att.getSource();
File f = new File( templatePath );
if(f.exists())
{
System.out.println("Template already exist,use existing one." + templatePath);
} else{
att.extractFile(templatePath);
}
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(templatePath));
FileOutputStream fileOut = new FileOutputStream(fileName);
XSSFSheet sheet1 = wb.getSheet("General");

Related

Apache POI xlsx update (write) working fine locally but not working when the xlsx is in Bitbucket

I am reading from Xlsx and writing to the same xlsx with my scenarios.
Both worked flawlessly when I was running Locally.
Then I pushed my project to bitbucket remote/origin and the xlsx file is inside the project folder with same path.
When I run through Jenkins, the reading from xlsx works fine but the writing/updating to xlsx is not working .
There is no error , just the output/result cells are blank.
Any idea what I am missing?
My code below:
public static String FILE_NAME = "test-data/TEST.xlsx";
XSSFWorkbook workbook_res=null;
Cell result_cell=null;
FileInputStream gl_res = null;
if(ExcpectedCondition==true) {
gl_res = new FileInputStream(new File(FILE_NAME));
workbook_res = new XSSFWorkbook(gl_res);
XSSFSheet result_sh = workbook_res.getSheet("Result");
result_cell = result_sh.getRow(1).getCell(5);
if (result_cell == null) {
result_cell = result_sh.getRow(1).createCell(5);
}
result_cell.setCellValue("Pass");
}else{
gl_res = new FileInputStream(new File(FILE_NAME));
workbook_res = new XSSFWorkbook(gl_res);
XSSFSheet result_sh = workbook_res.getSheet("Result");
result_cell = result_sh.getRow(1).getCell(5);
if (result_cell == null) {
result_cell = result_sh.getRow(1).createCell(5);
}
result_cell.setCellValue("Fail");
}
FileOutputStream out_res = new FileOutputStream(FILE_NAME);
//Write into Output stream
workbook_res.write(out_res);
//Close Output stream
out_res.close();
//Close Workbook
workbook_res.close();
gl_res.close();
}

The fomat and extension of "file.csv" doesn't match

I am trying to create the csv file with data as follows
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");
String fileLocation = null;
try {
// Set the header columns name for worksheet
createHeader(workbook, sheet);
// populate the data
createWorkbookRows(ker, sheet, kerList, AerList);
String tempDir = System.getProperty("java.io.tmpdir");
File file = new File(tempDir);
fileLocation = file.getAbsolutePath() + FileSystems.getDefault().getSeparator() + "MyData.csv";
FileOutputStream outputStream = new FileOutputStream(fileLocation);
workbook.write(outputStream);
}catch(Exception e){
}
With the above code I am able to generate the csv file, but when I try to open the file the below error it is showing
When I click on yes it is showing the data properly in excel file.
I have verified the csv file by opening in Notepad++ also it is showing in non understandable language instead of comma seperated.
Please suggest how can I solve the above issue.
POI writes a binary Excel file. Your extension "csv" is wrong. It should be "xlsx" because you use XSSFWorkbook.

Excel file gets corrupted when i change the value of any cell in the header (Columns Title)

I'm trying to read and Xslx file using JAVA POI, edit it and save it. But when i change any cell value on row#0 (Header) the output file gets corrupted.
Excel can recover the file by repairing, Table from /xl/tables/table.xm .
FileInputStream file = new FileInputStream("inventorywithbin431.xlsx");
XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(file);
XSSFSheet rawdata = workbook.getSheetAt(0);
String[] headers = new String[] { "ItemCodeNoFRU","Company","ItemCode","ItemName","ItemGroup","PartNumber","Warehouse","Segment","Bin","WareHouseQty","OnOrder","IsCommited","BinQty","Cost","BinExtCost"};
//Set Header
for(int i=0;i<14;i++)
rawdata.getRow(0).getCell(i).setCellValue(headers[i]);
file.close();
//write changes
try (
FileOutputStream output_file = new FileOutputStream("inventorywithbin431.xlsx")) {
workbook.write(output_file);
output_file.flush();
output_file.close();
}

editing an excel sheet using Apache poi

i am trying to edit an excel sheet using apache poi,
I have a worksheet having 2 rows and 5 columns
if second cell of row 2 is null then it should add a 3rd row ,
but i am getting a class not found exception, although the UpdateXLS is present in my D drive.
Here is the code:
public class New {
public static void main(String[] args){
try{
InputStream inp = new FileInputStream("D:\\UpdateXLS.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(2);
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("UpdateXLS.xls");
wb.write(fileOut);
fileOut.close();
}
catch(Exception e){
e.printStackTrace();
}
}
What should i do ?
From the comment , I can suggest you that check file exits on the path specified.
Use File
File f = new File("D:\\UpdateXLS.xls");
//check whether file exists or not to avoid any further exceptions
if(f.exists()){
Workbook wb = WorkbookFactory.create(f);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
......
.......
........
}
FileNotFoundException
Signals that an attempt to open the file denoted by a specified pathname has failed.
This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist.
It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.
When you are writing a file check your path
FileOutputStream fileOut = new FileOutputStream("UpdateXLS.xls");
Change to
FileOutputStream fileOut = new FileOutputStream("D:\\UpdateXLS.xls");
The problem seems to be at
FileOutputStream fileOut = new FileOutputStream("UpdateXLS.xls");
try to replace the line with:
FileOutputStream fileOut = new FileOutputStream("D:\\UpdateXLS.xls");

How to read embedded objects from a doc file?

I am trying to read embedded documents in a .doc file using POIFS file system. According to this there is an ObjectPool directory which contains all embedded documents especially for a doc file.
I found the directory but don't know the way to read these documents.
Please suggest any way to read these documents. If POIFS is not the suitable way then please suggest any other library.
My code is:
public static void ReadCSV(String fileName) throws IOException{
FileInputStream myInput = new FileInputStream(fileName);
POIFSFileSystem fs = new POIFSFileSystem(myInput);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
//the OLE2 Class Name of the object
System.out.println("Objects : "+ obj.getOLE2ClassName()+ " 2 .");
String oleName = obj.getOLE2ClassName();
if (oleName.equals("Worksheet")) {
// some code to process embedded excel file;
} else if (oleName.equals("Document")) {
System.out.println("Document");
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
HWPFDocument embeddedWordDocument = new HWPFDocument(dn,fs);
System.out.println("Doc : " + embeddedWordDocument.getRange().text());
// want to extract document not text into a doc file
//************************
FileOutputStream fos = new FileOutputStream("E:\\log.txt");
fos.write(text.getBytes());
//************************
} else if (oleName.equals("Presentation")) {
// some code to process embedded power point file;
} else {
// some code to process other kind of embedded files;
}
}
}

Categories

Resources