Jxl读写Excel。
/**
*
*/
package com.test.jxl;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* @author Administrator
* 利用jxl读写Excel。
*/
public class ReadExcel {
/**
* @param args
*/
public static void main(String[] args) {
// 源文件位置
String sourceName = "C:/Documents and Settings/Administrator/桌面/demo.xls";
// 保存到的位置
String saveName = "C:/Documents and Settings/Administrator/桌面/test.xls";
try {
// 获取文档对象
Workbook wb = Workbook.getWorkbook(new File(sourceName));
// 创建一个写的对象
WritableWorkbook newWb = Workbook.createWorkbook(new File(saveName));
// 获取所以的Sheet
Sheet[] sheets = wb.getSheets();
// 遍历集合
for (int i=0;i<sheets.length;i++) {
Sheet sheet = sheets[i];
// Sheet名字
String sheetName = sheet.getName();
// 设置新的文件的Sheet名字
WritableSheet newSheet = newWb.createSheet(sheetName, i);
// 获取行数
int rowNum = sheet.getRows();
// 获取列数
int colNum = sheet.getColumns();
for (int m = 0;m < colNum; m++) {
if (m == 4) {
newSheet.setColumnView(m, 40);
}
else {
newSheet.setColumnView(m, 20);
}
}
for (int x=0;x<rowNum;x++) {
if (x == 0) {
// 添加一行标题,并设置字体,红色字体,绿色背景
WritableFont font = new WritableFont(WritableFont.ARIAL,15,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);
WritableCellFormat format = new WritableCellFormat(font);
// 居中显示
format.setAlignment(Alignment.CENTRE);
// 设置背景色
format.setBackground(Colour.GREEN);
// 设置前景色
Label head = new Label(0,0,"通讯录",format);
head.setCellFormat(format);
newSheet.addCell(head);
// 合并单元格
newSheet.mergeCells(0, 0, colNum, 2);
}
for (int y=0;y<colNum;y++) {
// 得到单元格对象
Cell cell = sheet.getCell(y,x);
// 获取单元格的内容
String content = cell.getContents();
if (cell.getType() == CellType.DATE) {
// 原文件中日前是yyyy-m-d/yyyy-mm-d/yyyy-mm-dd
//这里读取到的是yy-m-d/yy-mm-d/yy-mm-dd
System.out.println("日前转化前:" + content);
// 下面将日期转化为yyyy-m-d/yyyy-mm-d/yyyy-m-dd/yyyy-mm-dd
String[] strArr = content.split("-");
String year = strArr[0];
String month = strArr[1];
String date = strArr[2];
String prefix = "";
if (year.length() == 2) {
if (Integer.valueOf(year) > 60) {
prefix = "19";
}
else {
prefix = "20";
}
}
content = prefix + year + "-" + month + "-" + date;
System.out.println("日期转化后:" + content);
}
Label label = null;
if (y == 4) { // 将地址一列设置为自动换行
// 设置列自动换行
WritableCellFormat wcf = new WritableCellFormat();
wcf.setWrap(true);
label = new Label(y,x+2,content,wcf);
}
else {
label = new Label(y,x+2, content);
}
//
newSheet.addCell(label);
}
}
}
// 写入到文件
newWb.write();
// 关闭文档
wb.close();
newWb.close();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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();
}
}
}