So, I am writing a code in which I imported a class with class name "Workbook" and function "createWorkBook". I asked the same question earlier but I wanted to add changes so I removed that before anyone could reply.
Anyways,
I am new to Java , interfaces and importing class.
I imported a package named "jxl" and I am using it. Here is my FULL code so far.
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
public class WriteExcel {
public static void main (String args[]) throws IOException , WriteException
{
try{
Workbook wb = new Workbook();
}
catch(WriteException e)
{
System.out.println("Sorry, failed! Keep on trying harder! :)");
}
}
}
ALL I am trying for past half an hour is trying to make an object "wb" in class "workbook" .
I followed bit of instructions from https://www.youtube.com/watch?v=A9866lBdmKo (importing of class).
I am getting an error for the link Workbook wb= new Workbook();
Cannot instantiate the Workbook type. I did some research turns out it is relating to some "interface". But the video didn't even talk about interface. I am new and would like some guidance. I just want to create one object.
So I got the file, thanks for ANY sort of input!
Workbook is an Anonymous Inner Type java class, whenever you want to instantiate that class, we'll need to override several methods. You will have to implement those methods. You can find more about Anonymous Inner Type java class here
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Having a look at the javadoc, it seems that Workbook is an abstract class, so you can't instanciate it, but it seems that it provides some staticmethods called createWorkbook() that you can use, like that :
Workbook wb = Workbook.createWorkbook(new File("/path/to/the/workbook/file"));
As workbook is an abstract class. The only way to create a workbook object is
String fileName = "file.xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
Creates a writable workbook with the given file name.
Also please refer to the below link that represent the workbook class and its methods
http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html
Related
I am sorry, I am absolutely new to JAVA and no (until now) nothing about JAVA, and only little to coding. I wanted to learn simple document creation with the XWPFDocument Library. I got a sample code from https://www.geeksforgeeks.org/java-program-to-write-a-paragraph-in-a-word-document/ but the code does not work.
I am also sorry for not being able to format the code as a code. I made spaces with my hand, but not everywhere. I know it's horrible, but I don't know how to do to properly. A have a brand new Mac and a microsoft keyboard which drives me crazy + I also do not know how to do it the advice strg + k
I get the error:
at GFG.main(docgen_V0.1.java:17)
js#MacBook-Pro-von-Jonathan ~ % /usr/bin/env /Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home/bin/java --enable-preview -XX:+Show
CodeDetailsInExceptionMessages -cp /private/var/folders/fq/f_v1db_d17qc749gnnmrbch00000gn/T/vscodesws_2d1b0/jdt_ws/jdt.ls-java-project/bin G
FG
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
XWPFDocument cannot be resolved to a type
XWPFDocument cannot be resolved to a type
XWPFParagraph cannot be resolved to a type
XWPFRun cannot be resolved to a type
XWPFRun cannot be resolved to a type
XWPFRun cannot be resolved to a type
The Code is:
public class docgen_V0.2 {
// Java Programming to Write a paragraph in a Word Document
// Importing required packages
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Create a blank document
XWPFDocument xwpfdocument = new XWPFDocument();
// Create a blank file at C:
File file = new File("/Users/Shared/Arbeit/Codes/Speachdoc/addParagraph.docx");
// Create a file output stream connection
FileOutputStream ostream
= new FileOutputStream(file);
/* Create a new paragraph using the document */
// CreateParagraph() method is used
// to instantiate a new paragraph
XWPFParagraph para = xwpfdocument.createParagraph();
// CreateRun method appends a new run to the
// paragraph created
XWPFRun xwpfrun = para.createRun();
// SetText sets the text to the run
// created using XWPF run
xwpfrun.setText(
"Geeks for Geeks is a computer science portal which aims "
+ "to provide all in one platform for learning and "
+ "practicing.We can learn multiple programming languages here. ");
// Write content set using XWPF classes available
xwpfdocument.write(ostream);
// Close connection
ostream.close();
}
}
}
I just tried to run it. I expect to create a document with a little paragraph that says:
" Geeks for Geeks is a computer science portal which aims to provide all in one platform for learning and practicing.We can learn multiple programming languages here."
What exactly it means when I call the constructor XSSFWorkbook(java.io.InputStream is). I am new to java, and I am looking for an answer in layman terms.
I am assuming that you are talking about the Apache POI API XSSFWorkbook. In this case, when you are calling the constructor, you are specifying the inputstream of an excel file.
for example
InputStream input = new FileInputStream("C:\\Users\\...\\MyExcelFile.xlsx");
That would be initializing a new InputStream, where you could pass that into the XSSFWorkbook
XSSFWorkbook myWorkbook = new XSSFWorkbook(input);
Then you could make changes/get information by using the various methods of the XSSFWorkbook class.
I am studying Java, Spring and POI. I see https://poi.apache.org/spreadsheet/quick-guide.html and follow it.
At "New WorkBook", next code exists.
Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
Running above code, it works well.
I found out that "Workbook" is NOT class BUT interface. I learned that interface HAVE TO be implemented. But I CANNOT found the implementation. Where is it implemented? What must I study to comprehend it?
Oficial Oracle tutorial about interfaces:
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
That is interface instance. Details: Using an Interface as a Type
In short, you can create instance of interface.
Case 1: for anonymous class.
Workbook wb = new Workbook() {
// Some implementation
}
Case 2: Use it as type. (Yes! your case!)
Workbook wb = new XSSFWorkbook();
see also: Strategy Design Pattern in Java. It is very useful idea.
Here I want to extract the data from .xlsx file and for that I already add the poi jar and created the reference of fileInputStream
package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class DemoExcel {
public static void main(String[] args) throws Exception{
File excel = new File("C:\\Users\\Devaditya\\Documents\\Book1.xlsx");
FileInputStream fis = null;
fis = new FileInputStream(excel);
System.out.println(fis.toString());
HSSFWorkbook wb = new HSSFWorkbook(fis);
System.out.println(wb.toString());
HSSFSheet sh = wb.getSheet("Data");
System.out.println(sh.toString());
}
}
Here i am getting the error:-
Exception in thread "main" java.io.IOException: Invalid header signature; read 0, expected -2226271756974174256
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:88)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:83)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:210)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:191)
at demo.DemoExcel.main(DemoExcel.java:23)
Lets start with types of WorkBook.
HSSFWorkbook
This is old binary proprietary Excel format, know by the extension .xls.
XSSFWorkbook
This is the new XML Excel format, known by the extension .xlsx.
So, you are using the wrong class.
In fact it would be better not to use a specific class at all, let POI work out what you have. Use a WorkbookFactory:
final Workbook workbook = WorkbookFactory.create(excel);
This is:
programming to the interface.
robust to changes in the type of workbook read, as long a POI supports it
faster and more efficient. POI can read the File piecemeal, when it needs to rather than having to slurp the whole workbook into memory.
Doesn't have the memory leak that you have when you don't close() the FileInputStream.
try to open the file in microsoft office Excel then a pop message will appear to inform you that that file you are trying to open isn't an excel file, and give you the choice to open it any way, so just open it and save it as excel file.
I have a Excel file that I have to read the file and go line by line and check the first column. Here is an example of the column headers
ISBN#13 Run Date Title Author Type
So I have to check each ISBN#13 and determine if it is an isbn#13, format it and write the whole line to a file. Then take all the ones that are not ISBN#13 and write them to a file.
So the question is how do I check the column "ISBN#13" and how do I write each row to a file. It would be another excel file.
There is also xslx4j (part of docx4j) if you are working with xlsx only (ie not the legacy binary .xls) and prefer to use jaxb
The Apache poi (Poor Obfuscation Implementation) project is made for reading from and writing to Excel files in Java:
http://poi.apache.org/
Code example:
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateWorkBook
{
public static void main(String[] args)throws Exception
{
//Create Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create file system using specific name
FileOutputStream out = new FileOutputStream(
new File("createworkbook.xlsx"));
//write operation workbook using file out object
workbook.write(out);
out.close();
System.out.println("createworkbook.xlsx written successfully");
}
}