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);
}
Related
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();
}
}
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/)
Hi I am trying to test logging into bookmyshow.
Firstly I am reading data from excel sheet to write in username and password but while executing I am getting error that
Error: Main method not found in class Test_Pack1.Bookmyshow, please
define the main method as: public static void main(String[] args)
or a JavaFX application class must extend
javafx.application.Application.
I am providing written program code as well.please look into this and correct me if I am wrong thank you.
package Test_Pack1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
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;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class Bookmyshow {
//public static void main()
{
System.setProperty("webdriver.chrome.driver",
"G:\\Selenium_Test\\lib\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://in.bookmyshow.com/hyderabad?wzrk_source=Google&wzrk_medium=CPC_Branded&wzrk_campaign=Book_My_Show_Search|RLSA&gclid=CjwKEAiAu6DBBRDDr6-e_6698E0SJACvuxnyBrs85Juwsx_cWiHXrPnS0eKFhZoQmo_nCYgopyDSIxoCGCHw_wcB");
driver.manage().window().maximize();
driver.findElement((By.className("email-input")));
// driver.findElement(By.id("FirstName"));
driver.findElement(By.xpath(".//*[#id='iUserName']")).notifyAll();
}
public static void main(String[] args) throws IOException
{
FileInputStream istream=new FileInputStream("G:\\Selenium_Test\\hari.xlsx");
XSSFWorkbook wrkbook=new XSSFWorkbook(istream);
XSSFSheet sheet1=wrkbook.getSheet("Sheet1");
XSSFRow row=null;
XSSFCell cell=null;
HashMap<String, String> dataHmap = new HashMap<String, String>();
String PropertyName,value;
int lastRowNumber=sheet1.getLastRowNum();
}
}
The way you have commented earlier section is not correct.
First of all, your code which needs to be invoked as a starting point should be included in main method.You can invoke multiple methods in turn from inside main method. I have tried to change your code bit and it should work. Let me know if you face any issue further
package com.automation.servicenow.config;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
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;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class Bookmyshow {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver",
"G:\\Selenium_Test\\lib\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://in.bookmyshow.com/hyderabad?wzrk_source=Google&wzrk_medium=CPC_Branded&wzrk_campaign=Book_My_Show_Search|RLSA&gclid=CjwKEAiAu6DBBRDDr6-e_6698E0SJACvuxnyBrs85Juwsx_cWiHXrPnS0eKFhZoQmo_nCYgopyDSIxoCGCHw_wcB");
driver.manage().window().maximize();
driver.findElement((By.className("email-input")));
// driver.findElement(By.id("FirstName"));
driver.findElement(By.xpath(".//*[#id='iUserName']")).notifyAll();
FileInputStream istream=new FileInputStream("G:\\Selenium_Test\\hari.xlsx");
XSSFWorkbook wrkbook=new XSSFWorkbook(istream);
XSSFSheet sheet1=wrkbook.getSheet("Sheet1");
XSSFRow row=null;
XSSFCell cell=null;
HashMap<String, String> dataHmap = new HashMap<String, String>();
String PropertyName,value;
int lastRowNumber=sheet1.getLastRowNum();
}
}
I am trying to create a simple Java Jax-RS based webservice which accepts the file in a byte[] format or as a blob and pushes it to an FTP folder. This service is used by Salesforce to push files to directories via this Java API. For testing i have hosted this application in https://ftptransfer.herokuapp.com/myresource
I need to create a class which basically accepts files from external systems
package com.example;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.POST;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
#Path("sendFiles")
public class FileTransfer {
#POST
#Consumes({MediaType.MULTIPART_FORM_DATA})
public String uploadPdfFile( )
{
return "Uploaded successfully";
}
}
Can some help with any good reference to achieve this, as i have tried many approaches from different blogs but none helped me much.
I am able to achieve this in the following way
import org.apache.commons.net.ftp.FTPClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import java.io.InputStream;
import javax.ws.rs.POST;
#Path("sendFiles")
public class FileTransfer {
#POST
#Path("file")
#Consumes("*/*")
public String uploadPdfFile(InputStream fileInputStream)
{
FTPClient client = new FTPClient();
boolean login;
FileInputStream fis = null;
int read = 0;
byte[] bytes = new byte[1024];
OutputStream out;
try {
File file=new File("testhm001.txt");
out = new FileOutputStream(file);
while ((read = fileInputStream.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
out.close();
InputStream in = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out1 = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out1.append(line);
}
return "done";
}
}
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);