教你如何分分钟完爆“Bean 导入 Excel”

来源:互联网 发布:js动态添加表格行 编辑:程序博客网 时间:2024/06/05 20:17
import java.io.File;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.util.List;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.Colour;import jxl.format.UnderlineStyle;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;import jxl.write.biff.RowsExceededException;/** * 将任意的 List(List(bean)) 形式的 bean 集合,展示到 xlsxPath (xlsx/xls 文件)<BR> *  * @version 1.0 (2014-8-14) * @author asdf *  */public class Beans2ExcelWithSheets {/** * 一个 sheet */private static WritableWorkbook wwb;/** * 用于写入 excel 文件的流 */private static OutputStream os;/** * excel 文件 */private static File file;/** * 用 单 sheet 的 excel 显示出来 <br> *  * @param result *            需要显示的结果集 * @param xlsxPath *            xlsx文件的绝对路径 */public static <T> void showInExcelWithSheet(List<T> result, String xlsxPath) {try {file = new File(xlsxPath);file.createNewFile();os = new FileOutputStream(file);wwb = Workbook.createWorkbook(os);createSheet(result, wwb, "Sheet", 0);wwb.write();} catch (Exception e) {e.printStackTrace();} finally {if (wwb != null)try {wwb.close();} catch (WriteException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (os != null)try {os.close();} catch (IOException e) {e.printStackTrace();}}}}/** * 用 多 sheet 的 excel 显示出来 <br> *  * @param result *            需要显示的结果集 * @param xlsxPath *            xlsx文件的绝对路径 */public static <T> void showInExcelWithSheets(List<List<T>> result,String xlsxPath) {try {file = new File(xlsxPath);file.createNewFile();os = new FileOutputStream(file);wwb = Workbook.createWorkbook(os);int count = 0;for (List<T> t : result) {createSheet(t, wwb, "Sheet" + (count + 1), count);count++;}wwb.write();} catch (Exception e) {e.printStackTrace();} finally {if (wwb != null)try {wwb.close();} catch (WriteException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (os != null)try {os.close();} catch (IOException e) {e.printStackTrace();}}}}/** * 创建 sheet <br> *  * @param array_t *            需要显示的结果集 * @param wwb *            一个 sheet * @param title *            表题 * @param index *            第几个 sheet * @throws WriteException *             写入异常 * @throws RowsExceededException *             行溢出异常 */private static <T> void createSheet(List<T> array_t, WritableWorkbook wwb,String title, int index) throws WriteException,RowsExceededException {WritableSheet ws = wwb.createSheet(title, index);WritableFont wf = new WritableFont(WritableFont.TAHOMA, 12,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);WritableCellFormat wcf = new WritableCellFormat(wf);wcf.setBackground(jxl.format.Colour.YELLOW2);wcf.setAlignment(Alignment.CENTRE);if (!array_t.isEmpty()) {Class<? extends Object> clazz = array_t.get(0).getClass();Field[] fields = clazz.getDeclaredFields();int length = fields.length;Label l = null;String field_name = "";for (int i = 0; i < length; i++) {field_name = fields[i].getName();int width = field_name.length() * 2;l = new Label(i, 0, field_name, wcf);ws.setColumnView(i, width > 10 ? width : 10);ws.addCell(l);}int count = 1;WritableCellFormat wcf2 = null;Method[] methods = clazz.getMethods();int m_length = methods.length;for (T bean : array_t) {int count_col = 0;/** * WritableCellFormat 不可复用 */wcf2 = new WritableCellFormat(new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK));if (count % 2 == 0)wcf2.setBackground(Colour.GRAY_25);elsewcf2.setBackground(Colour.WHITE);Method m = null;String m_name = "";for (int i = 0; i < m_length; i++) {m = methods[i];m_name = m.getName();/** * 从 Object 中继承的 getClass() 需要剔除掉 */if (m_name.startsWith("get") && !"getClass".equals(m_name)) {try {Object o = m.invoke(bean);String s = String.valueOf(o);l = new Label(count_col, count, s, wcf2);} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}ws.addCell(l);count_col++;}}count++;}}}}


相关资源:http://yun.baidu.com/s/1o6FOLEq


0 0
原创粉丝点击