import java.io.File;
import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List;import org.apache.log4j.Logger;
import jxl.Workbook;
import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook;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; }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; }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/13剛剛看見一片文章寫得確實不錯,自己也敲了一遍。放在這裡分享
源代碼地址:https://www.cnblogs.com/linjiqin/p/3540266.html
1、jxl导入/导出excel案例,黏贴即可运行
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;istuList=queryStudentList(); if(stuList!=null && !stuList.isEmpty()){ for(int i=0; i stuList=new ArrayList (); // 遍历每行每列的单元格 for(int i=0;i queryStudentList(){ List stuList=new ArrayList (); 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; } }}