I have a source src Excel file to read, and a destination dst file to overwrite for edited changes after reading src. With k=true, the following code fails with POIXMLException. With k=false, both files are written.
Exception in thread "main" org.apache.poi.ooxml.POIXMLException:
java.io.EOFException: Unexpected end of ZLIB input stream
at org.apache.poi.ooxml.POIXMLDocument.getProperties(POIXMLDocument.java:146)
at org.apache.poi.ooxml.POIXMLDocument.write(POIXMLDocument.java:225)
at Test.main(Test.java:26)
What should I do to keep intact the 'src' file?, hopefully not changing the ´Worksheet´ and ´Sheet´ classes.
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class Test {
static Workbook wb;
static Sheet sh;
public static void main(String[] args) throws Exception {
Files.copy(Paths.get("src\\src.xlsx"),Paths.get("src\\dst.xlsx"),
StandardCopyOption.REPLACE_EXISTING);
File fl; boolean k=true;
if (k)
fl= new File("src\\src.xlsx"); //Fails
else
fl = new File("src\\dst.xlsx"); //Do Not Fails
wb = WorkbookFactory.create(fl);
sh = wb.getSheetAt(0);
Row row = sh.getRow(1);
row.getCell(1).setCellValue("Test");
OutputStream fos = new FileOutputStream("src\\src.xlsx");
wb.write(fos); // [FAILS]
wb.close();
}
}
Related
My Requirement is to read data from input excel file and write it in output excel file I am using JAVA 8 AND APACHE POI
During the write process I am changing the date format for a particular column from mm/dd/yyyy to dd/mm/yyyy
With below code its setting current date in excel, I want to set same values of input excel in output excel but with new format defined.
Any help would be really appreciated
Below is Updated code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class DateFormatDemo1{
public static void main( String[] args ) throws IOException, InvalidFormatException, ParseException
{
OPCPackage pkg = OPCPackage.open(new File("C:\\DateFormatIssue\\SourceFile\\S.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook(pkg);
XSSFCreationHelper createHelper = wb.getCreationHelper();
XSSFSheet sheet = wb.getSheetAt(0); // Create spreadsheet in workbook
Iterator<Row> iterator = sheet.iterator();
Cell cell = null;
Row row=null;
row=iterator.next();
int pos=11;
while(iterator.hasNext())
{
row=iterator.next();
cell= row.getCell(pos-1);
XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle();
cellStyle.setDataFormat( createHelper.createDataFormat().getFormat("M/dd/yyyy")); // set the format of the date
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");
Date d=null;
/** below if block converts dates which are in format d/M/yyyy (15/04/2017) to M/d/yyyy (4/15/2017)**/
if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
d= sdf.parse(cell.getStringCellValue());
cell.setCellValue(d);
cell.setCellStyle(cellStyle);
}
else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
cell.setCellValue(cell.getNumericCellValue());
}
}
FileOutputStream outFile =new FileOutputStream(new File("C:\\DateFormatIssue\\OutputFile\\output.xlsx"));
wb.write(outFile);
wb.close();
outFile.close();
pkg.close();
}}
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.write.*;
import jxl.write.Number.*;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class reader {
public static void main(String args[]) throws IOException, WriteException, BiffException {
String inputFile = "C:\\Users\\Chemeris\\Documents\\Book1.xls";
File inputWorkbook = new File(inputFile);
Workbook w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet(0);
//Ading a label
Label label = new Label(2,2, "Hello");
sheet.addCell(label);
w.write();
w.close();
}
}
I get a cannot find symbol error on ".addCell" and on ".write".
If you can help me solve this problem or offer another solution to wrting to an existing excel file I would be most thankfull
You may try to use ApachePOI to get the same result. There are bunch of documents https://www.tutorialspoint.com/apache_poi/)
I use NetBeans 8. I got problem after compiling this simple code:
package file;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
public class File {
public static void main(String[] args) throws FileNotFoundException, IOException
{ Workbook wb = new XSSFWorkbook();
String name = "charlie.xlsx";
FileOutputStream fileOut = new FileOutputStream(name);
wb.write(fileOut);
fileOut.close();
I'm total rookie in Java so basically I rewrote that code from Apache POI documentation, trying to understand how it works. Well - it works fine until I try to open the output file in MS Excel - because then I get a message that file cannot be open cause it's corrupt.
What went wrong?
You need to create a Sheet. Add this to your code and it will work.
wb.createSheet("Test1");
This is my code:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
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 Reader {
public static void read_excel() {
File excel = new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
}
This results in the following error message:
error: cannot find symbol
File excel = new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
symbol: class File
location: class reader
I have set the CLASSPATH for the jar files of the Apache POI library. Here is the content of the CLASSPATH varibale:
.;C:\Users\Username\Desktop\Code\Classes;C:\poi-3.12\poi-3.12-20150511.jar;C:\poi-3.12\poi-ooxml-3.12-20150511.jar;C:\poi-3.12\poi-ooxml-schemas-3.12-20150511.jar;C:\poi-3.12\ooxml-lib\xmlbeans-2.6.0.jar;C:\poi-3.12\lib\commons-codec-1.9.jar;C:\poi-3.12\lib\commons-logging-1.1.3.jar;C:\poi-3.12\lib\junit-4.12.jar; C:\poi-3.12\lib\log4j-1.2.17.jar;C:\poi-3.12\poi-examples-3.12-20150511.jar;C:\poi-3.12\poi-excelant-3.12-20150511.jar;C:\poi-3.12\poi-scratchpad-3.12-20150511.jar
I don't understand why the programme does not compile !
Add import statement for File
import java.io.File;
try with the following code
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.io.File;
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 Reader {
public static void read_excel() throws FileNotFoundException {
File excel = new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
}
Ok, this code here finally did not produce an error message:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.File;
import java.util.*;
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 Reader {
public static void read_excel() throws FileNotFoundException, IOException {
File excel = new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
}
So I am receiving error messages as the XSSFWorkbook import appears to be incompatible with the final line. The code as it is gives the error message that package "org.apache.poi.xssf.usermodel.XSSFWorkbook" does not exist. When I remove the import, the last line then gets the error message that I should create an XSSFWorkbook class. I am currently using the Apache 3.7 utilmodel file in order to import the XSSFWorkbook class. Any help would be most appreciated. I am trying to create a program that reads data from an excel file.
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ReadExcel {
private static final String FILE_PATH =
"/Users/riveraaz/Desktop/FantasyPros_2015_Overall.numbers";
public static List getPlayersFromExcel() {
List PlayerList = new ArrayList();
FileInputStream fis = null;
try {
fis = new FileInputStream(FILE_PATH);
Workbook workbook = new XSSFWorkbook(fis);