Java POI 导出EXCEL经典实现 Java导出Excel
轉(zhuǎn)自http://blog.csdn.net/evangel_z/article/details/7332535
在web開(kāi)發(fā)中,有一個(gè)經(jīng)典的功能,就是數(shù)據(jù)的導(dǎo)入導(dǎo)出。特別是數(shù)據(jù)的導(dǎo)出,在生產(chǎn)管理或者財(cái)務(wù)系統(tǒng)中用的非常普遍,因?yàn)檫@些系統(tǒng)經(jīng)常要做一些報(bào)表打印的工作。而數(shù)據(jù)導(dǎo)出的格式一般是EXCEL或者PDF,我這里就用兩篇文章分別給大家介紹下。(注意,我們這里說(shuō)的數(shù)據(jù)導(dǎo)出可不是數(shù)據(jù)庫(kù)中的數(shù)據(jù)導(dǎo)出!么誤會(huì)啦^_^)
? ? ? ???呵呵,首先我們來(lái)導(dǎo)出EXCEL格式的文件吧。現(xiàn)在主流的操作Excel文件的開(kāi)源工具有很多,用得比較多的就是Apache的POI及JExcelAPI。這里我們用Apache POI!我們先去Apache的大本營(yíng)下載POI的jar包:http://poi.apache.org/ ,我這里使用的是3.0.2版本。
? ? ? ??將3個(gè)jar包導(dǎo)入到classpath下,什么?忘了怎么導(dǎo)包?不會(huì)吧!好,我們來(lái)寫一個(gè)導(dǎo)出Excel的實(shí)用類(所謂實(shí)用,是指基本不用怎么修改就可以在實(shí)際項(xiàng)目中直接使用的!)。我一直強(qiáng)調(diào)做類也好,做方法也好,一定要通用性和靈活性強(qiáng)。下面這個(gè)類就算基本貫徹了我的這種思想。那么,熟悉許老師風(fēng)格的人應(yīng)該知道,這時(shí)候該要甩出一長(zhǎng)串代碼了。沒(méi)錯(cuò),大伙請(qǐng)看:
1 import java.util.Date; 2 3 public class Student 4 { 5 private long id; 6 private String name; 7 private int age; 8 private boolean sex; 9 private Date birthday; 10 11 public Student() 12 { 13 } 14 15 public Student(long id, String name, int age, boolean sex, Date birthday) 16 { 17 this.id = id; 18 this.name = name; 19 this.age = age; 20 this.sex = sex; 21 this.birthday = birthday; 22 } 23 24 public long getId() 25 { 26 return id; 27 } 28 29 public void setId(long id) 30 { 31 this.id = id; 32 } 33 34 public String getName() 35 { 36 return name; 37 } 38 39 public void setName(String name) 40 { 41 this.name = name; 42 } 43 44 public int getAge() 45 { 46 return age; 47 } 48 49 public void setAge(int age) 50 { 51 this.age = age; 52 } 53 54 public boolean getSex() 55 { 56 return sex; 57 } 58 59 public void setSex(boolean sex) 60 { 61 this.sex = sex; 62 } 63 64 public Date getBirthday() 65 { 66 return birthday; 67 } 68 69 public void setBirthday(Date birthday) 70 { 71 this.birthday = birthday; 72 } 73 74 } 1 public class Book 2 { 3 private int bookId; 4 private String name; 5 private String author; 6 private float price; 7 private String isbn; 8 private String pubName; 9 private byte[] preface; 10 11 public Book() 12 { 13 } 14 15 public Book(int bookId, String name, String author, float price, 16 String isbn, String pubName, byte[] preface) 17 { 18 this.bookId = bookId; 19 this.name = name; 20 this.author = author; 21 this.price = price; 22 this.isbn = isbn; 23 this.pubName = pubName; 24 this.preface = preface; 25 } 26 27 public int getBookId() 28 { 29 return bookId; 30 } 31 32 public void setBookId(int bookId) 33 { 34 this.bookId = bookId; 35 } 36 37 public String getName() 38 { 39 return name; 40 } 41 42 public void setName(String name) 43 { 44 this.name = name; 45 } 46 47 public String getAuthor() 48 { 49 return author; 50 } 51 52 public void setAuthor(String author) 53 { 54 this.author = author; 55 } 56 57 public float getPrice() 58 { 59 return price; 60 } 61 62 public void setPrice(float price) 63 { 64 this.price = price; 65 } 66 67 public String getIsbn() 68 { 69 return isbn; 70 } 71 72 public void setIsbn(String isbn) 73 { 74 this.isbn = isbn; 75 } 76 77 public String getPubName() 78 { 79 return pubName; 80 } 81 82 public void setPubName(String pubName) 83 { 84 this.pubName = pubName; 85 } 86 87 public byte[] getPreface() 88 { 89 return preface; 90 } 91 92 public void setPreface(byte[] preface) 93 { 94 this.preface = preface; 95 } 96 }上面這兩個(gè)類一目了然,就是兩個(gè)簡(jiǎn)單的javabean風(fēng)格的類。再看下面真正的重點(diǎn)類:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;import javax.swing.JOptionPane;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFComment; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor;/*** 利用開(kāi)源組件POI3.0.2動(dòng)態(tài)導(dǎo)出EXCEL文檔 轉(zhuǎn)載時(shí)請(qǐng)保留以下信息,注明出處!* * @author leno* @version v1.0* @param <T>* 應(yīng)用泛型,代表任意一個(gè)符合javabean風(fēng)格的類* 注意這里為了簡(jiǎn)單起見(jiàn),boolean型的屬性xxx的get器方式為getXxx(),而不是isXxx()* byte[]表jpg格式的圖片數(shù)據(jù)*/ public class ExportExcel<T> {public void exportExcel(Collection<T> dataset, OutputStream out){exportExcel("測(cè)試POI導(dǎo)出EXCEL文檔", null, dataset, out, "yyyy-MM-dd");}public void exportExcel(String[] headers, Collection<T> dataset,OutputStream out){exportExcel("測(cè)試POI導(dǎo)出EXCEL文檔", headers, dataset, out, "yyyy-MM-dd");}public void exportExcel(String[] headers, Collection<T> dataset,OutputStream out, String pattern){exportExcel("測(cè)試POI導(dǎo)出EXCEL文檔", headers, dataset, out, pattern);}/*** 這是一個(gè)通用的方法,利用了JAVA的反射機(jī)制,可以將放置在JAVA集合中并且符號(hào)一定條件的數(shù)據(jù)以EXCEL 的形式輸出到指定IO設(shè)備上* * @param title* 表格標(biāo)題名* @param headers* 表格屬性列名數(shù)組* @param dataset* 需要顯示的數(shù)據(jù)集合,集合中一定要放置符合javabean風(fēng)格的類的對(duì)象。此方法支持的* javabean屬性的數(shù)據(jù)類型有基本數(shù)據(jù)類型及String,Date,byte[](圖片數(shù)據(jù))* @param out* 與輸出設(shè)備關(guān)聯(lián)的流對(duì)象,可以將EXCEL文檔導(dǎo)出到本地文件或者網(wǎng)絡(luò)中* @param pattern* 如果有時(shí)間數(shù)據(jù),設(shè)定輸出格式。默認(rèn)為"yyy-MM-dd"*/@SuppressWarnings("unchecked")public void exportExcel(String title, String[] headers,Collection<T> dataset, OutputStream out, String pattern){// 聲明一個(gè)工作薄HSSFWorkbook workbook = new HSSFWorkbook();// 生成一個(gè)表格HSSFSheet sheet = workbook.createSheet(title);// 設(shè)置表格默認(rèn)列寬度為15個(gè)字節(jié)sheet.setDefaultColumnWidth((short) 15);// 生成一個(gè)樣式HSSFCellStyle style = workbook.createCellStyle();// 設(shè)置這些樣式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);style.setBorderTop(HSSFCellStyle.BORDER_THIN);style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 生成一個(gè)字體HSSFFont font = workbook.createFont();font.setColor(HSSFColor.VIOLET.index);font.setFontHeightInPoints((short) 12);font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 把字體應(yīng)用到當(dāng)前的樣式 style.setFont(font);// 生成并設(shè)置另一個(gè)樣式HSSFCellStyle style2 = workbook.createCellStyle();style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);style2.setBorderRight(HSSFCellStyle.BORDER_THIN);style2.setBorderTop(HSSFCellStyle.BORDER_THIN);style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 生成另一個(gè)字體HSSFFont font2 = workbook.createFont();font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 把字體應(yīng)用到當(dāng)前的樣式 style2.setFont(font2);// 聲明一個(gè)畫圖的頂級(jí)管理器HSSFPatriarch patriarch = sheet.createDrawingPatriarch();// 定義注釋的大小和位置,詳見(jiàn)文檔HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,0, 0, 0, (short) 4, 2, (short) 6, 5));// 設(shè)置注釋內(nèi)容comment.setString(new HSSFRichTextString("可以在POI中添加注釋!"));// 設(shè)置注釋作者,當(dāng)鼠標(biāo)移動(dòng)到單元格上是可以在狀態(tài)欄中看到該內(nèi)容.comment.setAuthor("leno");// 產(chǎn)生表格標(biāo)題行HSSFRow row = sheet.createRow(0);for (short i = 0; i < headers.length; i++){HSSFCell cell = row.createCell(i);cell.setCellStyle(style);HSSFRichTextString text = new HSSFRichTextString(headers[i]);cell.setCellValue(text);}// 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行Iterator<T> it = dataset.iterator();int index = 0;while (it.hasNext()){index++;row = sheet.createRow(index);T t = (T) it.next();// 利用反射,根據(jù)javabean屬性的先后順序,動(dòng)態(tài)調(diào)用getXxx()方法得到屬性值Field[] fields = t.getClass().getDeclaredFields();for (short i = 0; i < fields.length; i++){HSSFCell cell = row.createCell(i);cell.setCellStyle(style2);Field field = fields[i];String fieldName = field.getName();String getMethodName = "get"+ fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);try{Class tCls = t.getClass();Method getMethod = tCls.getMethod(getMethodName,new Class[]{});Object value = getMethod.invoke(t, new Object[]{});// 判斷值的類型后進(jìn)行強(qiáng)制類型轉(zhuǎn)換String textValue = null;// if (value instanceof Integer) {// int intValue = (Integer) value;// cell.setCellValue(intValue);// } else if (value instanceof Float) {// float fValue = (Float) value;// textValue = new HSSFRichTextString(// String.valueOf(fValue));// cell.setCellValue(textValue);// } else if (value instanceof Double) {// double dValue = (Double) value;// textValue = new HSSFRichTextString(// String.valueOf(dValue));// cell.setCellValue(textValue);// } else if (value instanceof Long) {// long longValue = (Long) value;// cell.setCellValue(longValue);// }if (value instanceof Boolean){boolean bValue = (Boolean) value;textValue = "男";if (!bValue){textValue = "女";}}else if (value instanceof Date){Date date = (Date) value;SimpleDateFormat sdf = new SimpleDateFormat(pattern);textValue = sdf.format(date);}else if (value instanceof byte[]){// 有圖片時(shí),設(shè)置行高為60px;row.setHeightInPoints(60);// 設(shè)置圖片所在列寬度為80px,注意這里單位的一個(gè)換算sheet.setColumnWidth(i, (short) (35.7 * 80));// sheet.autoSizeColumn(i);byte[] bsValue = (byte[]) value;HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,1023, 255, (short) 6, index, (short) 6, index);anchor.setAnchorType(2);patriarch.createPicture(anchor, workbook.addPicture(bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));}else{// 其它數(shù)據(jù)類型都當(dāng)作字符串簡(jiǎn)單處理textValue = value.toString();}// 如果不是圖片數(shù)據(jù),就利用正則表達(dá)式判斷textValue是否全部由數(shù)字組成if (textValue != null){Pattern p = Pattern.compile("^//d+(//.//d+)?$");Matcher matcher = p.matcher(textValue);if (matcher.matches()){// 是數(shù)字當(dāng)作double處理 cell.setCellValue(Double.parseDouble(textValue));}else{HSSFRichTextString richString = new HSSFRichTextString(textValue);HSSFFont font3 = workbook.createFont();font3.setColor(HSSFColor.BLUE.index);richString.applyFont(font3);cell.setCellValue(richString);}}}catch (SecurityException e){e.printStackTrace();}catch (NoSuchMethodException e){e.printStackTrace();}catch (IllegalArgumentException e){e.printStackTrace();}catch (IllegalAccessException e){e.printStackTrace();}catch (InvocationTargetException e){e.printStackTrace();}finally{// 清理資源 }}}try{workbook.write(out);}catch (IOException e){e.printStackTrace();}}public static void main(String[] args){// 測(cè)試學(xué)生ExportExcel<Student> ex = new ExportExcel<Student>();String[] headers ={ "學(xué)號(hào)", "姓名", "年齡", "性別", "出生日期" };List<Student> dataset = new ArrayList<Student>();dataset.add(new Student(10000001, "張三", 20, true, new Date()));dataset.add(new Student(20000002, "李四", 24, false, new Date()));dataset.add(new Student(30000003, "王五", 22, true, new Date()));// 測(cè)試圖書ExportExcel<Book> ex2 = new ExportExcel<Book>();String[] headers2 ={ "圖書編號(hào)", "圖書名稱", "圖書作者", "圖書價(jià)格", "圖書ISBN", "圖書出版社", "封面圖片" };List<Book> dataset2 = new ArrayList<Book>();try{BufferedInputStream bis = new BufferedInputStream(new FileInputStream("V://book.bmp"));byte[] buf = new byte[bis.available()];while ((bis.read(buf)) != -1){// }dataset2.add(new Book(1, "jsp", "leno", 300.33f, "1234567","清華出版社", buf));dataset2.add(new Book(2, "java編程思想", "brucl", 300.33f, "1234567","陽(yáng)光出版社", buf));dataset2.add(new Book(3, "DOM藝術(shù)", "lenotang", 300.33f, "1234567","清華出版社", buf));dataset2.add(new Book(4, "c++經(jīng)典", "leno", 400.33f, "1234567","清華出版社", buf));dataset2.add(new Book(5, "c#入門", "leno", 300.33f, "1234567","湯春秀出版社", buf));OutputStream out = new FileOutputStream("E://a.xls");OutputStream out2 = new FileOutputStream("E://b.xls");ex.exportExcel(headers, dataset, out);ex2.exportExcel(headers2, dataset2, out2);out.close();JOptionPane.showMessageDialog(null, "導(dǎo)出成功!");System.out.println("excel導(dǎo)出成功!");}catch (FileNotFoundException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}} }?寫完之后,如果您不是用eclipse工具生成的Servlet,千萬(wàn)別忘了在web.xml上注冊(cè)這個(gè)Servelt。而且同樣的,拷貝一張小巧的圖書圖片命名為book.jpg放置到當(dāng)前WEB根目錄的/WEB-INF/下。部署好web工程,用瀏覽器訪問(wèn)Servlet看下效果吧!是不是下載成功了。呵呵,您可以將下載到本地的excel報(bào)表用打印機(jī)打印出來(lái),這樣您就大功告成了。完事了我們就思考:我們發(fā)現(xiàn),我們做的方法,不管是本地調(diào)用,還是在WEB服務(wù)器端用Servlet調(diào)用;不管是輸出學(xué)生列表,還是圖書列表信息,代碼都幾乎一樣,而且這些數(shù)據(jù)我們很容器結(jié)合后臺(tái)的DAO操作數(shù)據(jù)庫(kù)動(dòng)態(tài)獲取。恩,類和方法的通用性和靈活性開(kāi)始有點(diǎn)感覺(jué)了。好啦,祝您學(xué)習(xí)愉快!
?
?
簡(jiǎn)單版 無(wú)excel格式樣式設(shè)置 ?及excel下載
1 package com.howbuy.uaa.utils; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 import java.lang.reflect.Field; 11 import java.lang.reflect.InvocationTargetException; 12 import java.lang.reflect.Method; 13 import java.text.SimpleDateFormat; 14 import java.util.Collection; 15 import java.util.Date; 16 import java.util.Iterator; 17 import java.util.regex.Matcher; 18 import java.util.regex.Pattern; 19 20 import javax.servlet.http.HttpServletResponse; 21 22 import org.apache.poi.hssf.usermodel.HSSFCell; 23 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 24 import org.apache.poi.hssf.usermodel.HSSFFont; 25 import org.apache.poi.hssf.usermodel.HSSFRichTextString; 26 import org.apache.poi.hssf.usermodel.HSSFRow; 27 import org.apache.poi.hssf.usermodel.HSSFSheet; 28 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 29 30 31 public class ExportExcel<T> { 32 public void exportExcel(String title,Collection<T> dataset, OutputStream out) { 33 exportExcel(title, null, dataset, out, "yyyy-MM-dd"); 34 } 35 36 public void exportExcel(String title,String[] headers, Collection<T> dataset, 37 OutputStream out) { 38 exportExcel(title, headers, dataset, out, "yyyy-MM-dd"); 39 } 40 41 @SuppressWarnings("unchecked") 42 public void exportExcel(String title, String[] headers,Collection<T> dataset, OutputStream out, String pattern) { 43 // 聲明一個(gè)工作薄 44 HSSFWorkbook workbook = new HSSFWorkbook(); 45 // 生成一個(gè)表格 46 HSSFSheet sheet = workbook.createSheet(title); 47 // 設(shè)置表格默認(rèn)列寬度為15個(gè)字節(jié) 48 sheet.setDefaultColumnWidth((short) 15); 49 // 生成一個(gè)樣式 50 HSSFCellStyle style = workbook.createCellStyle(); 51 // 生成一個(gè)字體 52 HSSFFont font = workbook.createFont(); 53 font.setFontHeightInPoints((short) 12); 54 // 把字體應(yīng)用到當(dāng)前的樣式 55 style.setFont(font); 56 // 產(chǎn)生表格標(biāo)題行 57 HSSFRow row = sheet.createRow(0); 58 for (short i = 0; i < headers.length; i++) { 59 HSSFCell cell = row.createCell(i); 60 cell.setCellStyle(style); 61 HSSFRichTextString text = new HSSFRichTextString(headers[i]); 62 cell.setCellValue(text); 63 } 64 // 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行 65 Iterator<T> it = dataset.iterator(); 66 int index = 0; 67 while (it.hasNext()) { 68 index++; 69 row = sheet.createRow(index); 70 T t = (T) it.next(); 71 // 利用反射,根據(jù)javabean屬性的先后順序,動(dòng)態(tài)調(diào)用getXxx()方法得到屬性值 72 Field[] fields = t.getClass().getDeclaredFields(); 73 for (short i = 0; i < fields.length; i++) { 74 HSSFCell cell = row.createCell(i); 75 cell.setCellStyle(style); 76 Field field = fields[i]; 77 String fieldName = field.getName(); 78 String getMethodName = "get" 79 + fieldName.substring(0, 1).toUpperCase() 80 + fieldName.substring(1); 81 try { 82 Class tCls = t.getClass(); 83 Method getMethod = tCls.getMethod(getMethodName, 84 new Class[] {}); 85 Object value = getMethod.invoke(t, new Object[] {}); 86 // 判斷值的類型后進(jìn)行強(qiáng)制類型轉(zhuǎn)換 87 String textValue = null; 88 if (value instanceof Integer) { 89 int intValue = (Integer) value; 90 cell.setCellValue(intValue); 91 } 92 else if (value instanceof Long) { 93 long longValue = (Long) value; 94 cell.setCellValue(longValue); 95 } 96 else if (value instanceof Boolean) { 97 boolean bValue = (Boolean) value; 98 textValue = "1"; 99 if (!bValue) { 100 textValue = "0"; 101 } 102 } else if (value instanceof Date) { 103 Date date = (Date) value; 104 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 105 textValue = sdf.format(date); 106 } else { 107 // 其它數(shù)據(jù)類型都當(dāng)作字符串簡(jiǎn)單處理 108 if(value == null) 109 { 110 textValue = ""; 111 } 112 else { 113 textValue = value.toString(); 114 } 115 116 } 117 118 if (textValue != null) { 119 Pattern p = Pattern.compile("^//d+(//.//d+)?$"); 120 Matcher matcher = p.matcher(textValue); 121 if (matcher.matches()) { 122 // 是數(shù)字當(dāng)作double處理 123 cell.setCellValue(Double.parseDouble(textValue)); 124 } else { 125 HSSFRichTextString richString = new HSSFRichTextString( 126 textValue); 127 richString.applyFont(font); 128 cell.setCellValue(richString); 129 } 130 } 131 } catch (SecurityException e) { 132 e.printStackTrace(); 133 } catch (NoSuchMethodException e) { 134 e.printStackTrace(); 135 } catch (IllegalArgumentException e) { 136 e.printStackTrace(); 137 } catch (IllegalAccessException e) { 138 e.printStackTrace(); 139 } catch (InvocationTargetException e) { 140 e.printStackTrace(); 141 } finally { 142 // 清理資源 143 } 144 } 145 146 } 147 try { 148 workbook.write(out); 149 } catch (IOException e) { 150 e.printStackTrace(); 151 } 152 } 153 154 public void downloadExcel(String path, HttpServletResponse response) { 155 try { 156 // path是指欲下載的文件的路徑。 157 File file = new File(path); 158 String name = file.getName(); 159 // 取得文件名。 160 String filename = java.net.URLEncoder.encode(name,"utf-8"); 161 // 清空response 162 response.reset(); 163 // 設(shè)置response的Header 164 response.addHeader("Content-Disposition", "attachment;filename=" 165 + new String(filename.getBytes())); 166 response.addHeader("Content-Length", "" + file.length()); 167 OutputStream toClient = new BufferedOutputStream( 168 response.getOutputStream()); 169 response.setContentType("application/ms-excel;charset=gb2312"); 170 // 以流的形式下載文件。 171 InputStream fis = new BufferedInputStream(new FileInputStream(path)); 172 byte[] buffer = new byte[fis.available()]; 173 fis.read(buffer); 174 fis.close(); 175 toClient.write(buffer); 176 toClient.flush(); 177 toClient.close(); 178 } catch (IOException ex) { 179 ex.printStackTrace(); 180 } 181 } 182 }?
轉(zhuǎn)載于:https://www.cnblogs.com/dawnheaven/p/4462572.html
總結(jié)
以上是生活随笔為你收集整理的Java POI 导出EXCEL经典实现 Java导出Excel的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: (软件工程复习核心重点)第十二章软件项目
- 下一篇: 计组之存储系统:5、cache(cach