import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;html
import org.apache.log4j.Logger;java
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;apache
public class WriteExcelTable {
Logger log = Logger.getLogger(this.getClass().getName());
public String createWrokBook(String templateFile, String newFilePath, String newFileName, List<String[]> param){
String newFilePathName = newFilePath + "/" + newFileName;
try{
boolean b1 = creFile(newFilePathName);
if(b1){
FileOutputStream fileOutputStream = new FileOutputStream(new File(newFilePathName));
WritableWorkbook writableWorkbook = Workbook.createWorkbook(fileOutputStream);
WritableSheet writableSheet = creSheet(writableWorkbook, "sheet1", 0);
try{
for (int i = 0; i < param.size(); i++) {
for (int j = 0; j < ((String[])param.get(i)).length; j++) {
String tep = ((String[])param.get(i))[j];
Label label = creLabel(j, i, tep);
writableSheet.addCell(label);
}
}
writableWorkbook.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
writableWorkbook.close();
fileOutputStream.flush();
fileOutputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return newFilePathName;
}
private boolean creFile(String filePathName){
boolean mesg = false;
try {
File file = new File(filePathName);
if (!file.exists()) {
file.createNewFile();
}
mesg = true;
if (file.isFile())
mesg = true;
else
mesg = false;
}
catch (Exception e) {
e.printStackTrace();
}
return mesg;
}測試
private WritableSheet creSheet(WritableWorkbook workbook, String sheetName, int count){
WritableSheet sheet = null;
try {
sheet = workbook.createSheet(sheetName, count);
} catch (Exception e) {
e.printStackTrace();
}
return sheet;
}this
private Label creLabel(int col, int rol, String context){
Label label = null;
try {
label = new Label(col, rol, context);
} catch (Exception e) {
e.printStackTrace();
}
return label;
}excel
public static void main(String[] args)throws Exception{
try{
String[] arr1 = { "標題1", "標題2", "標題3", "標題4", "標題5", "標題6", "標題7", "標題8", "標題9" };
String[] arr2 = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
List list = new ArrayList();
list.add(arr1);
list.add(arr2);
WriteExcelTable w = new WriteExcelTable();
w.createWrokBook("", "D:\\", "123.xls", list);
System.out.println("over");
}catch (Exception e){
e.printStackTrace();
}
}
}
--補充2017/12/13orm
剛剛看見一片文章寫得確實不錯,本身也敲了一遍。放在這裡分享htm
源代碼地址:https://www.cnblogs.com/linjiqin/p/3540266.html對象
一、jxl導入/導出excel案例,黏貼便可運行blog
package junit.test; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableImage; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import org.apache.commons.lang3.math.NumberUtils; import org.junit.Test; /** * Excel導入導出 * * @author 林計欽 * @version 1.0 Feb 7, 2014 4:14:51 PM */ public class ExcelTest { /** * 導入(導入到內存) */ @Test public void importExcel() { Workbook book = null; try { book = Workbook.getWorkbook(new File("D:/test/測試.xls")); // 得到第一個工做表對象 Sheet sheet = book.getSheet(0); int rows=sheet.getRows(); int columns=sheet.getColumns(); // 遍歷每行每列的單元格 for(int i=0;i<rows;i++){ for(int j=0;j<columns;j++){ Cell cell = sheet.getCell(j, i); String result = cell.getContents(); if(j==0){ System.out.print("姓名:"+result+" "); } if(j==1){ System.out.print("年齡:"+result+" "); } if((j+1)%2==0){ System.out.println(); } } } System.out.println("========"); // 獲得第一列第一行的單元格 Cell cell1 = sheet.getCell(0, 0); String result = cell1.getContents(); System.out.println(result); System.out.println("========"); } catch (Exception e) { System.out.println(e); }finally{ if(book!=null){ book.close(); } } } /** * 導出(導出到磁盤) */ @Test public void exportExcel() { WritableWorkbook book = null; try { // 打開文件 book = Workbook.createWorkbook(new File("D:/test/測試.xls")); // 生成名爲"學生"的工做表,參數0表示這是第一頁 WritableSheet sheet = book.createSheet("學生", 0); // 指定單元格位置是第一列第一行(0, 0)以及單元格內容爲張三 Label label = new Label(0, 0, "張三"); // 將定義好的單元格添加到工做表中 sheet.addCell(label); // 保存數字的單元格必須使用Number的完整包路徑 jxl.write.Number number = new jxl.write.Number(1, 0, 30); sheet.addCell(number); // 寫入數據並關閉文件 book.write(); } catch (Exception e) { System.out.println(e); }finally{ if(book!=null){ try { book.close(); } catch (Exception e) { e.printStackTrace(); } } } } /** * 對象數據寫入到Excel */ @Test public void writeExcel() { WritableWorkbook book = null; try { // 打開文件 book = Workbook.createWorkbook(new File("D:/test/stu.xls")); // 生成名爲"學生"的工做表,參數0表示這是第一頁 WritableSheet sheet = book.createSheet("學生", 0); List<Student> stuList=queryStudentList(); if(stuList!=null && !stuList.isEmpty()){ for(int i=0; i<stuList.size(); i++){ sheet.addCell(new Label(0, i, stuList.get(i).getName())); sheet.addCell(new Number(1, i, stuList.get(i).getAge())); } } // 寫入數據並關閉文件 book.write(); } catch (Exception e) { System.out.println(e); }finally{ if(book!=null){ try { book.close(); } catch (Exception e) { e.printStackTrace(); } } } } /** * 讀取Excel數據到內存 */ @Test public void readExcel() { Workbook book = null; try { // 打開文件 book = Workbook.getWorkbook(new File("D:/test/stu.xls")); // 得到第一個工做表對象 Sheet sheet = book.getSheet(0); int rows=sheet.getRows(); int columns=sheet.getColumns(); List<Student> stuList=new ArrayList<Student>(); // 遍歷每行每列的單元格 for(int i=0;i<rows;i++){ Student stu = new Student(); for(int j=0;j<columns;j++){ Cell cell = sheet.getCell(j, i); String result = cell.getContents(); if(j==0){ stu.setName(result); } if(j==1){ stu.setAge(NumberUtils.toInt(result)); } if((j+1)%2==0){ stuList.add(stu); stu=null; } } } //遍歷數據 for(Student stu : stuList){ System.out.println(String.format("姓名:%s, 年齡:%s", stu.getName(), stu.getAge())); } } catch (Exception e) { System.out.println(e); }finally{ if(book!=null){ try { book.close(); } catch (Exception e) { e.printStackTrace(); } } } } /** * 圖片寫入Excel,只支持png圖片 */ @Test public void writeImg() { WritableWorkbook wwb = null; try { wwb = Workbook.createWorkbook(new File("D:/test/image.xls")); WritableSheet ws = wwb.createSheet("圖片", 0); File file = new File("D:\\test\\png.png"); //前兩位是起始格,後兩位是圖片佔多少個格,並不是是位置 WritableImage image = new WritableImage(1, 4, 6, 18, file); ws.addImage(image); wwb.write(); } catch (Exception e) { e.printStackTrace(); }finally{ if(wwb!=null){ try { wwb.close(); } catch (Exception e) { e.printStackTrace(); } } } } private List<Student> queryStudentList(){ List<Student> stuList=new ArrayList<Student>(); stuList.add(new Student("zhangsan", 20)); stuList.add(new Student("lisi", 25)); stuList.add(new Student("wangwu", 30)); return stuList; } public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } }