JxlDemo.java
package org.zbq.execl;
import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class JxlDemo {
public void write(String fileName) throws IOException, RowsExceededException, WriteException {
File file = new File(fileName);
WritableWorkbook workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet("sheet1", 0);
for(int row=0; row<10; row++) {
for(int col=0; col<10; col++) {
WritableCell cell = new Label(col, row, "date"+row+col);
sheet.addCell(cell);
}
}
workbook.write();
workbook.close();
}
public void read(String fileName) throws BiffException, IOException {
File file = new File(fileName);
Workbook workbook = Workbook.getWorkbook(file);
Sheet[] sheets = workbook.getSheets();
Sheet sheet_0 = sheets[0];
int rows = sheet_0.getRows();
int cols = sheet_0.getColumns();
for(int row=0; row<rows; row++) {
for(int col=0; col<cols; col++) {
System.out.printf("%10s", sheet_0.getCell(row, col).getContents());
}
System.out.println();
}
workbook.close();
}
}
PoiDemo.java
package org.zbq.execl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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 PoiDemo {
public void write(String fileName) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
for(int row=0; row<10; row++) {
HSSFRow rownum = sheet.createRow(row);
for(int col=0; col<10; col++) {
rownum.createCell(col).setCellValue("data" + row + col);
}
}
File xlsFile = new File(fileName);
FileOutputStream xlsStream = new FileOutputStream(xlsFile);
workbook.write(xlsStream);
workbook.close();
}
public void read(String fileName) throws EncryptedDocumentException, InvalidFormatException, IOException {
File xlsFile = new File(fileName);
Workbook workbook = WorkbookFactory.create(xlsFile);
Sheet sheet_0 = workbook.getSheetAt(0);
int rows = sheet_0.getLastRowNum() + 1;
Row tmp = sheet_0.getRow(0);
int cols = tmp.getPhysicalNumberOfCells();
for(int row=0; row<rows; row++) {
Row r = sheet_0.getRow(row);
for(int col=0; col<cols; col++) {
System.out.printf("%10s", r.getCell(col).getStringCellValue());
}
System.out.println();
}
}
}
Main.java
package org.zbq.execl;
import java.io.IOException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class Main {
public static void main(String[] args) throws RowsExceededException, WriteException, IOException, BiffException, EncryptedDocumentException, InvalidFormatException {
System.out.println("ssss");
JxlDemo jxl = new JxlDemo();
String fileName = "abc.xlsx";
// demo.write(fileName);
String filein = "binqiang.xlsx";
// jxl.read(filein);
PoiDemo poi = new PoiDemo();
// poi.write(fileName);
poi.read("binqiang.xlsx");
}
}
JXL 好像对2007版本的好像 不能读
maven pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.zbq</groupId>
<artifactId>excel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
</dependencies>
</project>