package jexcel.jxl.nimit;
import java.awt.Label;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExcelJxl {
/**
* #param args
* #throws IOException
* #throws BiffException
* #throws WriteException
* #throws RowsExceededException
*/
public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException {
// TODO Auto-generated method stub
ExcelJxl.WriteFile("D:\nimit.xls");
}
public static void WriteFile(String path) throws BiffException, IOException, RowsExceededException, WriteException{
Workbook wb=Workbook.getWorkbook(new File(path));
WritableWorkbook copy=Workbook.createWorkbook(new File("D:\temp.xls"),wb);
WritableSheet sheet = copy.getSheet(1);
WritableCell cell = sheet.getWritableCell(0,0);
String S="nimit";
if (cell.getType() == CellType.LABEL)
{
LabelCell l = (LabelCell) cell;
l.setString(S);
}
copy.write();
copy.close();
wb.close();
}
}
I have edited my program, and now it says that setString() The method setString(String) is undefined for the type LabelCell I read the Documentation, there is a method setString in the LabelCell type.
LabelCell is just an interface with only one method i.e getString() you can learn more about it here
You should use jxl.write.Label instead.
What you should exactly do is as follows
You should import the following file
import jxl.write.Label
Then following is the code for adding a cell at desired location to an excel file
Workbook existingWorkbook = Workbook.getWorkbook(new File(fileToEdit.getAbsolutePath()));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet(sheetName);
WritableCell cell;
Label l = new Label(currentColumn, currentRow, value);
cell = (WritableCell) l;
sheetToEdit.addCell(cell);
workbookCopy.write();
workbookCopy.close();
existingWorkbook.close();
currentColumn and currentRow define the index and value contains the String to be placed in that cell.
Hope it helps
Related
Getting java.lang.NullPointerException when i try to get the value of the
last row in a excel sheet using getLastRowNum() function.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excel{
public static File file;
public static FileInputStream input;
public static FileOutputStream output;
public static HSSFWorkbook book;
public static HSSFSheet sheet;
public static int value;
public Excel(String path) {
try {
file= new File(path);//creating file//
input=new FileInputStream(file);
book=new HSSFWorkbook(input);
sheet=book.getSheetAt(0);
output=new FileOutputStream(file);
}
catch(Exception e) {
e.getMessage();
}
}
public static void readData() {
int value =sheet.getLastRowNum();//trying to get the last row value//
System.out.println(value);
}
}
Driver class:
public class ExcelTest {
public static void main(String[] args) {
Excel excel = new Excel("C:/Users/HOME/Desktop/Sample.xlsx");
Excel.readData();
}
}
Please help on the issue.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class readExcel {
public static void main(String[] args) throws Exception{
readData("path to sample.xlsx");
}
public static void readData(String path) throws FileNotFoundException, IOException{
//getting xlsx file from path
FileInputStream file = new FileInputStream(new File(path));
XSSFWorkbook workbook = new XSSFWorkbook(file);
//pointing to particlar workbook
XSSFSheet spreadsheet = workbook.getSheetAt(0);
//getting no: of rows
int value=spreadsheet.getLastRowNum()+1;
System.out.println("total rows:"+value);
}
}
Please add dom4j-1.6.1,xmlbeans-2.3.0 jars along with the poi-3.9 jar file
i have successfully generated barcode using itext5. but the problem i am having now is whenever i try to scan generated barcode via xlab scaner model YT-760 it fails to read it.
BarcodeInTable
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class BarcodeInTable {
public static final String DEST = "D:/barcode_in_table.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new BarcodeInTable().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
String code = "675-FH-A12";
PdfContentByte cb = writer.getDirectContent();
PdfPTable table = new PdfPTable(1);
Barcode128 barcode128 = new Barcode128();
barcode128.setFont(null);
barcode128.setCode(code);
barcode128.setCodeType(Barcode128.CODE128);
Image barcode128Image = barcode128.createImageWithBarcode(cb,null,null);
PdfPCell cell = new PdfPCell();
cell.addElement(new Phrase("PO"+code));
cell.addElement(barcode128Image);
table.addCell(cell);
document.add(table);
document.close();
}
}
do i have to write codes to make printed barcode to make it readable ? or simply printing generated barcode will be scannable?
p.s i am printing barcode with sato SA408 printer to print barcode. even printing on plain paper and trying to scan won't work.
package poi;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
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;
class Demo
{
public static void main(String a[]) throws IOException
{
String temp ="C:/santosh_chikne/Selenium Script/test.xlsx";
FileInputStream fs = new FileInputStream(temp);
Workbook wb = Workbook.getWorkbook(fs);
}
}
try as following:
String temp ="C:/santosh_chikne/Selenium Script/test.xlsx";
FileInputStream fs = new FileInputStream(temp);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sh = wb.getSheet("Sheet1");
Row row = sh.getRow(1);
Cell cell = row.getCell(1);
String valueFromXl = cell.getStringCellValue();
I am reading excel sheet using java apache POI. After reading i want to write on the same row, The ode is reading perfectly but not writing I want to create 6th column in every row in each iteration.... here is the code
/**
* Created by Muhammad Hussain on 26/10/2016.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
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.util.Scanner;
/**
* A dirty simple program that reads an Excel file.
* #author www.codejava.net
*
*/
public class ReadExcel {
public static void main(String[] args) throws IOException {
String excelFilePath = "C:\\Users\\Muhammad Hussain\\Desktop\\Data-Collection.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(4);
Scanner input =new Scanner(System.in);
for (int rowIndex = 1; rowIndex <= 5; rowIndex++) {
Row row = firstSheet.getRow(rowIndex);
Cell cell = row.getCell(3);
String review = cell.getStringCellValue();
System.out.println(review);
String label =input.next();
row.createCell(6).setCellValue(label);
}
inputStream.close();
inputStream.close();
}
}
You are only reading the file and not writing it. You have to put something like this in the end:
FileOutputStream out = new FileOutputStream(new File("..."));
workbook.write(out);
out.close();
package jexcel.jxl.nimit;
import java.io.*;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.read.biff.File;
public class ExampleJxl {
/**
* #param args
*/
public static void main(String[] args)throws IOException, BiffException {
ExampleJxl.ExcelFile("D:/nimit.xls");
}
public static String ExcelFile(String path){
Workbook workbook = Workbook.getWorkbook(File(path));
Sheet sheet = workbook.getSheet(0);
Cell a1 = sheet.getCell(0,0);
Cell a2 = sheet.getCell(0,1);
String s1=a1.getContents();
String s2=a2.getContents();
System.out.println("My name is"+a1+"\t"+a2);
}
}
I don't understand why the File(path) show a error The method File(String) is undefined for the type ExampleJxl
I'm trying to print my name entered in the excel file.
Chang your code from
Workbook workbook = Workbook.getWorkbook(File(path));
to
Workbook workbook = Workbook.getWorkbook(new java.io.File(path));