Apache XSSF Workbook conflicting package errors - java

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);

Related

OutputStream Workbook Fails to Overwrite

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();
}
}

Can not write to excel file using JexcelAPI

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/)

Strange Behaviour in Eclipse 3.8.1

I'm a newbie here. I have a simple problem in ONE java source file: the row System.out.pritln(...) has been treated as an erroneous expression. Here's the code snippet:
package vk.gui;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class MatrixSheet1 {
Properties p;
File file;
Document document;
PdfWriter writer;
Image logo = null;
Image EANimg = null;
float mnoz = new Double(72/25.6).floatValue();
int IMG_WIDTH= new Double(35*mnoz).intValue();
int IMG_HEIGHT=new Double(35*mnoz).intValue();
String err=p.getProperty("cell.height");
System.out.println("Arrgh!"); ///-------------->ERROR!
float cell_Height = Float.parseFloat(p.getProperty("cell.height"))*mnoz;
float cell_Width = Float.parseFloat(p.getProperty("cell.width"))*mnoz;
The reported error is
Multiple markers at this line
Syntax error on token ""Arrgh!"", delete this token
Syntax error on token(s), misplaced construct(s)
The sout and sysout shortcuts do not work neither. In other existing source files of same package everything is OK, the shortcuts work and the expression does not trigger an error.
I tried to create another source file and copy/paste the content, but I got the same error. What and where went wrong?
I need the printing just for debugging, but this is a bit annoying symptom.
Thanks in advance.
This happens because you can use System.out.println() only inside methodes. If you would do something like this, it would work:
public class MatrixSheet1 {
Properties p;
File file;
Document document;
PdfWriter writer;
Image logo = null;
Image EANimg = null;
float mnoz = new Double(72/25.6).floatValue();
int IMG_WIDTH= new Double(35*mnoz).intValue();
int IMG_HEIGHT=new Double(35*mnoz).intValue();
String err=p.getProperty("cell.height");
systemMessage("Argh!");
float cell_Height = Float.parseFloat(p.getProperty("cell.height"))*mnoz;
float cell_Width = Float.parseFloat(p.getProperty("cell.width"))*mnoz;
private void systemMessage(String message){
System.out.println(message);
}
}

"error: cannot find symbol" using Apache POI

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);
}

Data driven testing in Selenium using POI and TestNG ..code getting error

Using Selenium, I have written the below code to read data from an Excel sheet and key in an email and password into Facebook's login page. Error is Unhandled expression to use try/catch. How can I resolve this?
package Excelpack;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ClassTwo {
WebDriver driver;
String username;
String password;
#Test(dataProvider="testdata")
public void testFireFox(String uname, String password1){
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(uname);
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys(password1);
}
#DataProvider(name="testdata")
public Object[][] TestDataFeed1()
{
FileInputStream fis=new FileInputStream("D:\\book3.xlsx");
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sh1= wb.getSheetAt(0);
int numrow = sh1.getLastRowNum()-sh1.getFirstRowNum();
int colnum =sh1.getRow(1).getLastCellNum()+1;
Object [][] facebookdata=new Object[numrow][colnum];
for(int i=0;i<numrow;i++)
{
facebookdata[i][0]=sh1.getRow(0).getCell(i).getStringCellValue();
facebookdata[i][1]=sh1.getRow(1).getCell(i).getStringCellValue();
}
return facebookdata;
}
#BeforeTest
public void Setup(){
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
}
}
If you are using any IDE like eclipse or intellijI then IDE would prompt you with the solution as to add "throws necessary exception" or write the block of code in try/catch. from the looks of the code it seems you would need to declare throws exception on your dataprovider TestDataFeed1.

Categories