package edu.zzuli.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* Copyright (C) 2010 tianshaojie
* 封装对excel的操作,包括本地读写excel和流中输出excel
* 该类用到了swt组件,请导入swt jar包,以及jxl.jar包
* @author tianshaojie
* @since 2010.4.20
* @version 1.0
*/
public class ExcelHandle {
/**
* 读取指定excel文件的第一个工作表的全部内容
* 按单元格行列顺序读取内容
* @return excel文件的单元格Cell内容数组
*/
public String[] readXLS() {
/*InputStream is = null;
Workbook workbook = null;
try {
FileDialog fd = new FileDialog(new Shell(), SWT.OPEN); // 新建文件对话框,选择文件,并设置为打开的方式
fd.setText("Open"); // 设置文件对话框的标题
fd.setFilterPath("C:/"); // 设置初始路径
String[] filterExt = { "*.xls" }; // 设置扩展名过滤
fd.setFilterExtensions(filterExt);
String path = fd.open();
if(path == null) //如果取消直接return
return null;
is = new FileInputStream(path); // 写入到FileInputStream
workbook = Workbook.getWorkbook(is); // 得到工作薄
Sheet[] sheet = workbook.getSheets(); // 得到工作薄中的工作表
int rows = sheet[0].getRows(); // 获取第一个工作表数据表行数
int colnums = sheet[0].getColumns(); // 获取第一个工作表列数
String[] rest = new String[rows * colnums];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < colnums; col++) {
Cell cell = sheet[0].getCell(col, row); // getCell得到工作表指定单元格
rest[col + row * colnums] = cell.getContents(); // 获得单元格内容类型为字符串
}
}
is.close();
workbook.close();
return rest;
} catch (Exception e) {
if (workbook != null)
workbook.close();
System.out.println("ExcelHandle.ReadXLS() err:" + e);
}*/
return null;
}
/**
* 保存excel对话框
* @param shell swt组件
* @return 返回保存路径,包括文件名
*/
/*public String getExcelSavePath(Shell shell) {
FileDialog fdlg = new FileDialog(shell, SWT.SAVE); // 新建文件对话框,选择文件,并设置为打开的方式
fdlg.setText("Open"); // 设置文件对话框的标题
fdlg.setFilterPath("C:/"); // 设置初始路径
String[] filterExt = { "*.xls" }; // 设置扩展名过滤
fdlg.setFilterExtensions(filterExt);
String fileName = null;
boolean done = false;
while (!done) {
fileName = fdlg.open();
if (fileName == null) { // 用户取消,返回
done = true;
} else {// 看文件是否存在
File file = new File(fileName);
if (file.exists()) {
MessageBox mb = new MessageBox(fdlg.getParent(),SWT.ICON_WARNING | SWT.YES | SWT.NO);
mb.setMessage(fileName + " 文件已经存在,是否覆盖?");
done = mb.open() == SWT.YES;
} else {
done = true;
}
}
}
return fileName;
}*/
/**
* 写入文件操作测试 一个是工作表的名称,另一个是工作表在工作薄中的位置
* @param savaPath 保存excel的路径和文件名
* @param sheetName 保存工作表的名称
* @param index 保存工作表的位置(工作表
* @param rows 工作表的行数
* @param contents 要保存的内容数组
*/
public void writeXLS(String savaPath, String sheetName, int index,int rows, String[] contents) {
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(new File(savaPath)); // 创建可写入的Excel工作薄
WritableSheet ws = workbook.createSheet(sheetName, index); // 创建可写入的Excel工作表
int cols = contents.length / rows; // 根据行数得出列数,在数据库里比较容易得到行数
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Label label = new Label(col, row,contents[col + row * cols]);
ws.addCell(label); // 添加Label对象,单元格内容
}
}
workbook.write(); // 写入excel
workbook.close(); // 关闭对象,释放资源
} catch (FileNotFoundException e) {
System.out.println("ExcelHandle.writeXLS()" + e);
} catch (IOException e) {
System.out.println("ExcelHandle.writeXLS()" + e);
} catch (RowsExceededException e) {
System.out.println("ExcelHandle.writeXLS()" + e);
} catch (WriteException e) {
System.out.println("ExcelHandle.writeXLS()" + e);
}
}
/**
* 输出后台生成的excel到客户端
* @param os 指向客户端的输出流
* @param rows excel的总行数(就是从数据库读出来记录的总条数)
* @param datas 所有数据(不要存对象,是对象中的具体信息值)
*/
public static void writeXLS(OutputStream os,int rows, List datas) {
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(os); // 创建可写入的Excel工作薄
WritableSheet ws = workbook.createSheet("Sheet1", 0); // 创建可写入的Excel工作表
int cols = datas.size() / rows; // 根据行数得出列数,在数据库里比较容易得到行数
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Label label = new Label(col, row,(String) datas.get(col + row * cols));
ws.addCell(label); // 添加Label对象,单元格内容
}
}
workbook.write(); // 写入excel
workbook.close(); // 关闭对象,释放资源
} catch (IOException e) {
System.out.println("ExcelHandle.writeXLS() - IOException" + e);
} catch (RowsExceededException e) {
System.out.println("ExcelHandle.writeXLS() - RowsExceededException" + e);
} catch (WriteException e) {
System.out.println("ExcelHandle.writeXLS() - IOException" + e);
}
}
public static void main(String[] args) {
/*
ExcelHandle eh = new ExcelHandle();
// 读excel test
String[] str = eh.readXLS();
if(str != null)
for (int i = 0; i < str.length; i++)
System.out.println(str[i]);
// 写excel test
String path = eh.getExcelSavePath(new Shell());
String[] contents = new String[] { "a", "b", "c", "d", "1", "2", "3","5" };
if(path != null)
eh.writeXLS(path, "Test Sheet 1", 0, 2, contents);*/
}
}