Java对Excel的一些操作
上一篇 /
下一篇 2013-01-18 09:35:26
/ 个人分类:Java学习
Java对Excel的一些操作用法, 使用的是jxl.jar包,
代码参考如下:
import
java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ExcelOperation {
public
String fileName; //excel file path
public
String sheetNameOrIndex; //sheet name or index
public
ExcelOperation(){
sheetNameOrIndex = "0";
}
public
String[] getExcelAllColumnNames() throws Exception {
String[]
colNames = null;
Sheet sheet
= null;
try {
InputStream
is = new FileInputStream(fileName);
Workbook wb = Workbook.getWorkbook(is);
String regex = "\\d+";
if(sheetNameOrIndex.matches(regex)){
sheet =
wb.getSheet(Integer.parseInt(sheetNameOrIndex));}
else{
sheet =
wb.getSheet(sheetNameOrIndex);
}
int rows = sheet.getRows();
int cols = sheet.getColumns();
System.out.println("rows = " + rows);
System.out.println("cols = " + cols);
colNames = new String[cols];
for (int i = 0; i < cols; i++) {