引导语:我们都知道Excel 可以进行各种数据的处理、统计分析和辅助决策操作,那么在Java中又是如何操作Excel 表格的呢,以下是小编分享给大家的,欢迎阅读!
利用Java Excel API ,下载地址:jexcelapi.rar
下面给出一段读取数据的例子代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package excel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
*
* @author Wei.Liu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void mainString[] args {
try {
InputStream is = new FileInputStream"d:\\test.xls";
jxl.Workbook rwb = Workbook.getWorkbookis;
Sheet rs = rwb.getSheet0;
//取第一行,第一列的元素
Cell c00 = rs.getCell0, 0;
String strc00 = c00.getContents;
//取第一行,第二列的元素
Cell c10 = rs.getCell1,0;
String strc10= c10.getContents;
System.out.printlnstrc00+" "+c00.getType.toString;
System.out.printlnstrc10+" "+c10.getType.toString;
//获取sheet的个数
System.out.printlnrwb.getNumberOfSheets;
Sheet [] sheets =rwb.getSheets;
forint i=0;i
System.out.printlnrwb.getSheeti.getName;
}
//获得列数
System.out.printlnrwb.getSheet0.getColumns;
//获得每列的.元素
Cell [] cell = rwb.getSheet0.getColumn0;
//获得总行数
System.out.printlnrwb.getSheet0.getRows;
//获得行元素
Cell [] cell2 = rwb.getSheet0.getRow0;
//关闭对象
rwb.close;
} catch Exception ex {
Logger.getLoggerMain.class.getName.logLevel.SEVERE, null, ex;
}
}
}
再给出一段,创建Excel表格的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package excel;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Number;
/**
*
* @author Wei.Liu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void mainString[] args {
try {
//创建EXCEL表格
WritableWorkbook wwb = Workbook.createWorkbooknew File"d:\\test.xls";
//创建工作表
WritableSheet ws = wwb.createSheet"Liu.Wei",0;
//创建单元格
ws.addCellnew Label0,0,"Hello World";
//添加设置字体格式的单元格
WritableFont wfc = new WritableFontWritableFont.ARIAL,15,WritableFont.BOLD,true;
WritableCellFormat wcff= new WritableCellFormatwfc;
Label labelcf = new Label1,0,"Format text",wcff;
ws.addCelllabelcf;
//向单元格中写入数字
Number labelN = new Number0,1,12345;
ws.addCelllabelN;
wwb.write;
wwb.close;
} catch Exception ex {
Logger.getLoggerMain.class.getName.logLevel.SEVERE, null, ex;
}
}
}
最后给出一段更新表格的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package excel;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.CellType;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
*
* @author Wei.Liu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void mainString[] args {
try {
Workbook rw = Workbook.getWorkbooknew File"d:\\test.xls";
WritableWorkbook wwb = Workbook.createWorkbooknew File"d:\\test.xls",rw;
//修改单元格的值
WritableSheet ws = wwb.getSheet0;
WritableCell wc = ws.getWritableCell0,0;
ifwc.getType == CellType.LABEL{
Label l= Labelwc;
l.setString"Modified!!!";
}
wwb.write;
wwb.close;
rw.close;
} catch Exception ex {
Logger.getLoggerMain.class.getName.logLevel.SEVERE, null, ex;
}
}
}