所需JAR: jxl.jar
package
exceldemo.BO;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io. * ;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.Sheet;
import jxl.Cell;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.Label;
import jxl.write.WriteException;
import jxl.write.WritableCell;
import jxl.write.WritableImage;
import jxl.write.WritableFont;
import jxl.format.UnderlineStyle;
import jxl.write.WritableCellFormat;
public class WriteExcel {
public WriteExcel() {
}
/**
* 讀取Excel文件
*/
public void readExcel(){
try {
InputStream is = new FileInputStream( " c:\\ss.xls " );
Workbook workbook = Workbook.getWorkbook(is); // 構(gòu)建Workbook對象, 只讀Workbook對象
System.out.println( " 工作表個數(shù): " + workbook.getNumberOfSheets());
Sheet[] sheet = workbook.getSheets(); // 獲取Sheet表
int columns = sheet[ 0 ].getColumns();
int rows = sheet[ 0 ].getRows();
System.out.println( " 共 " + rows + " 行 " + columns + " 列 " );
for ( int i = 0 ;i < rows;i ++ ){
for ( int j = 0 ;j < columns;j ++ ){
Cell cell00 = sheet[ 0 ].getCell(j,i);
System.out.print(cell00.getContents());
}
System.out.println( "" );
}
workbook.close();
is.close();
} catch (FileNotFoundException ex) {ex.printStackTrace();
} catch (BiffException ex) {ex.printStackTrace();
} catch (IOException ex) {ex.printStackTrace();}
}
/**
* 寫Excel文件
*/
public void writeExcel(){
File newExcel = new File( " c:\\newExcel.xls " );
try {
WritableWorkbook wwb = Workbook.createWorkbook(newExcel); // 創(chuàng)建可寫入的Excel工作薄
// Method 2:將WritableWorkbook直接寫入到輸出流
/*
OutputStream os = new FileOutputStream(targetfile);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
*/
WritableSheet ws = wwb.createSheet( " TheFirstSheet " , 0 ); // 創(chuàng)建Excel工作表
// 普通文本
Label label00 = new Label( 0 , 0 , " derek " );
ws.addCell(label00);
WritableCell wc = new Label( 1 , 0 , " man " );
ws.addCell(wc);
// 圖片
WritableImage wi = new WritableImage( 1 , 5 , 1 , 3 , new File( " c:\\300.PNG " )); // 參數(shù)表示(列位置,行位置,寬占列數(shù),高占行數(shù),圖片路徑)
ws.addImage(wi);
// 格式化
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10 ,WritableFont.BOLD, false ,UnderlineStyle.DOUBLE,jxl.format.Colour.RED);
WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
Label labelCFC = new jxl.write.Label( 2 , 0 , " This is a Label Cell " , wcfFC);
ws.addCell(labelCFC);
// 寫入Exel工作表
wwb.write();
// 關(guān)閉Excel工作薄對象
wwb.close();
} catch (IOException ex) {ex.printStackTrace();}
catch (WriteException ex) {
/** @todo Handle this exception */
}
}
public static void main(String[] args) {
WriteExcel writeexcel = new WriteExcel();
writeexcel.writeExcel();
}
}
import java.io.InputStream;
import java.io.FileInputStream;
import java.io. * ;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.Sheet;
import jxl.Cell;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.Label;
import jxl.write.WriteException;
import jxl.write.WritableCell;
import jxl.write.WritableImage;
import jxl.write.WritableFont;
import jxl.format.UnderlineStyle;
import jxl.write.WritableCellFormat;
public class WriteExcel {
public WriteExcel() {
}
/**
* 讀取Excel文件
*/
public void readExcel(){
try {
InputStream is = new FileInputStream( " c:\\ss.xls " );
Workbook workbook = Workbook.getWorkbook(is); // 構(gòu)建Workbook對象, 只讀Workbook對象
System.out.println( " 工作表個數(shù): " + workbook.getNumberOfSheets());
Sheet[] sheet = workbook.getSheets(); // 獲取Sheet表
int columns = sheet[ 0 ].getColumns();
int rows = sheet[ 0 ].getRows();
System.out.println( " 共 " + rows + " 行 " + columns + " 列 " );
for ( int i = 0 ;i < rows;i ++ ){
for ( int j = 0 ;j < columns;j ++ ){
Cell cell00 = sheet[ 0 ].getCell(j,i);
System.out.print(cell00.getContents());
}
System.out.println( "" );
}
workbook.close();
is.close();
} catch (FileNotFoundException ex) {ex.printStackTrace();
} catch (BiffException ex) {ex.printStackTrace();
} catch (IOException ex) {ex.printStackTrace();}
}
/**
* 寫Excel文件
*/
public void writeExcel(){
File newExcel = new File( " c:\\newExcel.xls " );
try {
WritableWorkbook wwb = Workbook.createWorkbook(newExcel); // 創(chuàng)建可寫入的Excel工作薄
// Method 2:將WritableWorkbook直接寫入到輸出流
/*
OutputStream os = new FileOutputStream(targetfile);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
*/
WritableSheet ws = wwb.createSheet( " TheFirstSheet " , 0 ); // 創(chuàng)建Excel工作表
// 普通文本
Label label00 = new Label( 0 , 0 , " derek " );
ws.addCell(label00);
WritableCell wc = new Label( 1 , 0 , " man " );
ws.addCell(wc);
// 圖片
WritableImage wi = new WritableImage( 1 , 5 , 1 , 3 , new File( " c:\\300.PNG " )); // 參數(shù)表示(列位置,行位置,寬占列數(shù),高占行數(shù),圖片路徑)
ws.addImage(wi);
// 格式化
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10 ,WritableFont.BOLD, false ,UnderlineStyle.DOUBLE,jxl.format.Colour.RED);
WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
Label labelCFC = new jxl.write.Label( 2 , 0 , " This is a Label Cell " , wcfFC);
ws.addCell(labelCFC);
// 寫入Exel工作表
wwb.write();
// 關(guān)閉Excel工作薄對象
wwb.close();
} catch (IOException ex) {ex.printStackTrace();}
catch (WriteException ex) {
/** @todo Handle this exception */
}
}
public static void main(String[] args) {
WriteExcel writeexcel = new WriteExcel();
writeexcel.writeExcel();
}
}