对excel操作主要有两种方式,一种是通过jxl,一种是通过poi,本文主要讲述通过jxl对excel进行简单的操作,读取Excel,新建Excel,更改Excel内容
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class excelMain {
static String[][] name = { { "十", "八", "九" }, { "四", "五", "六" } };
public static void main(String[] args) {
String filename = "E:\\ceshi.xls";
// setExcel(name, filename);
// try {
// String[][] neirong = getExcel(filename);
// for (int i = 0; i < neirong.length; i++)
// for (int j = 0; j < neirong.length; j++)
// System.out.println(neirong[i][j]);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
updateExcel(name, filename);
}
/**
* 读取excel表
*
* @param address
* excel地址
* @return
* @throws IOException
*/
public static String[][] getExcel(String address) throws IOException {
try {
Workbook workbook = Workbook.getWorkbook(new File(address));
Sheet sheet = workbook.getSheet(0);
String[][] excel = new String[sheet.getRows()][sheet.getColumns()];
for (int i = 0; i < sheet.getRows(); i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
Cell cell = sheet.getCell(i, j);
excel[i][j] = cell.getContents();
}
}
workbook.close();
return excel;
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 创建excel表
*
* @param excel
* 往excel中存放的二维字符串数组
* @param address
* 存放excel的地址
* @return
* @throws IOException
* @throws WriteException
* @throws RowsExceededException
*/
public static boolean setExcel(String[][] excel, String address) {
// 创建工作薄
WritableWorkbook workbook;
try {
workbook = Workbook.createWorkbook(new File(address));
// 创建新的一页
WritableSheet sheet = workbook.createSheet("sheel1", 0);
for (int i = 0; i < excel.length; i++) {
for (int j = 0; j < excel.length; j++) {
// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行左边,第三个参数为内容
Label label = new Label(i, j, excel[i][j]);
sheet.addCell(label);
}
}
workbook.write();
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
/**
* 更改excel表
*
* @param excel
* 需要更新的字符串数组
* @param address
* 文件的地址
* @return
*/
public static boolean updateExcel(String[][] excel, String address) {
try {
Workbook workbook = Workbook.getWorkbook(new File(address));
WritableWorkbook workbooks = Workbook.createWorkbook(new File(
address), workbook);
WritableSheet sheet = workbooks.getSheet(0);
for (int i = 0; i < excel.length; i++) {
for (int j = 0; j < excel.length; j++) {
if (excel[i][j] != null && excel[i][j] != "") {
// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行左边,第三个参数为内容
Label label = new Label(i, j, excel[i][j]);
sheet.addCell(label);
}
}
}
workbooks.write();
workbooks.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}