jExcelAPI是一個韓國人寫的java操作excel的工具,jExcelAPI對中文支持非常好,API是純Java的,並不依賴Windows系統,即使運行在Linux下,它同樣能夠正確的處理Excel文件,另外需要說明的是,這套API對圖形和圖表的支持很有限,而且僅僅識別PNG格式.
搭建環境
將下載後的文件解包,得到jxl.jar,放入classpath.
基本操作
package com.WebExcel.dao;
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;
public class Excel {
/**
* 生成excel文件(文件標題欄與文件內容一定要對應)
* @param os
* @param title(excel文件標題欄)
* @param lists(excel文件內容)
* @throws IOException
* @throws RowsExceededException
* @throws WriteException
*/
public static void writeExcel(OutputStream os, String[] title, List lists) throws IOException, RowsExceededException, WriteException {
// 創建可以寫入的Excel工作薄(默認運行生成的文件在tomcat/bin下 )
WritableWorkbook wwb = Workbook.createWorkbook(os);
// 生成工作表,(name:First Sheet,參數0表示這是第一頁)
WritableSheet sheet = wwb.createSheet("First Sheet", 0);
// 開始寫入第一行(即標題欄)
for (int i=0; i<title.length; i++) {
// 用於寫入文本內容到工作表中去
Label label = null;
// 在Label對象的構造中指明單元格位置(參數依次代表列數、行數、內容 )
label = new Label(i, 0, title[i]);
// 將定義好的單元格添加到工作表中
sheet.addCell(label);
}
// 開始寫入內容
for (int row=0; row<lists.size(); row++) {
// 獲取一條(一行)記錄
List list = (List) lists.get(row);
// 數據是文本時(用label寫入到工作表中)
for (int col=0; col<list.size(); col++) {
String listvalue = (String) list.get(col).toString();
Label label = null;
label = new Label(col, row+1, listvalue);
sheet.addCell(label);
}
}
/*
生成一個保存數字的單元格,必須使用Number的完整包路徑,否則有語法歧義,值爲789.123
jxl.write.Number number = new jxl.write.Number(col, row, 555.12541);
sheet.addCell(number);
*/
/*
生成一個保存日期的單元格,必須使用DateTime的完整包路徑,否則有語法歧義,值爲new Date()
jxl.write.DateTime date = new jxl.write.DateTime(col, row, new java.util.Date());
sheet.addCell(date);
*/
// 寫入數據
wwb.write();
// 關閉文件
wwb.close();
// 關閉輸出流
os.close();
}
}
package com.WebExcel.dao;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
public class Files {
/**
* 向客戶端下載文件,彈出下載框.
*
* @param response(HttpServletResponse)
* @param file(需要下載的文件)
* @param isDel(下載完成後是否刪除該文件)
* @throws IOException
*/
public static void exportFile(HttpServletResponse response, File file, boolean isDel) throws IOException {
OutputStream out = null;
InputStream in = null;
// 獲得文件名
String filename = URLEncoder.encode(file.getName(), "UTF-8");
// 定義輸出類型(下載)
response.setContentType("application/force-download");
response.setHeader("Location", filename);
// 定義輸出文件頭
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
out = response.getOutputStream();
in = new FileInputStream(file.getPath());
byte[] buffer = new byte[1024];
int i = -1;
while ((i = in.read(buffer)) != -1) {
out.write(buffer, 0, i);
}
in.close();
out.close();
if (isDel) {
//刪除文件,刪除前關閉所有的Stream.
file.delete();
}
}
}
package com.WebExcel.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.WebExcel.dao.Excel;
import com.WebExcel.dao.Files;
public class ReportExcel extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 創建當前日子
Date date = new Date();
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// 格式化日期(產生文件名)
String filename = sdf.format(date);
// 創建文件
File f = new File("D://" + filename + ".xls");
f.createNewFile();
//標題數組
String title[] = {"編號","地市","大學編號","大學名稱","總保網數","定製總數","退訂總數","(2010-07-09至2010-07-09)總保網數","(2010-07-09至2010-07-09)定製總數","(2010-07-09至2010-07-09)退訂總數"};
//組成list
//測試數字
List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
//測試日期
List<Date> list2 = new ArrayList<Date>();
list2.add(new Date());
list2.add(new Date());
list2.add(new Date());
//測試英文
List<String> list3 = new ArrayList<String>();
list3.add("a");
list3.add("b");
list3.add("c");
//測試漢字
List<String> list4 = new ArrayList<String>();
list4.add("一");
list4.add("二");
list4.add("三");
List<List> lists = new ArrayList<List>();
lists.add(list1);
lists.add(list2);
lists.add(list3);
lists.add(list4);
// 生成excel文件(保存在服務器機上)
try {
Excel.writeExcel(new FileOutputStream(f), title, lists);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 導出文件(下載到客戶機上,並刪除服務器機上的excel文件)
Files.exportFile(response, f, true);
}
}