|
| 1 | +package com.shidian.excel; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.util.Log; |
| 5 | +import android.widget.Toast; |
| 6 | + |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import jxl.Workbook; |
| 16 | +import jxl.WorkbookSettings; |
| 17 | +import jxl.format.Colour; |
| 18 | +import jxl.write.Label; |
| 19 | +import jxl.write.WritableCell; |
| 20 | +import jxl.write.WritableCellFormat; |
| 21 | +import jxl.write.WritableFont; |
| 22 | +import jxl.write.WritableSheet; |
| 23 | +import jxl.write.WritableWorkbook; |
| 24 | +import jxl.write.WriteException; |
| 25 | + |
| 26 | +public class ExcelUtils { |
| 27 | + public static WritableFont arial14font = null; |
| 28 | + |
| 29 | + public static WritableCellFormat arial14format = null; |
| 30 | + public static WritableFont arial10font = null; |
| 31 | + public static WritableCellFormat arial10format = null; |
| 32 | + public static WritableFont arial12font = null; |
| 33 | + public static WritableCellFormat arial12format = null; |
| 34 | + |
| 35 | + public final static String UTF8_ENCODING = "UTF-8"; |
| 36 | + public final static String GBK_ENCODING = "GBK"; |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等... |
| 41 | + */ |
| 42 | + public static void format() { |
| 43 | + try { |
| 44 | + arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD); |
| 45 | + arial14font.setColour(jxl.format.Colour.LIGHT_BLUE); |
| 46 | + arial14format = new WritableCellFormat(arial14font); |
| 47 | + arial14format.setAlignment(jxl.format.Alignment.CENTRE); |
| 48 | + arial14format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); |
| 49 | + arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW); |
| 50 | + |
| 51 | + arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD); |
| 52 | + arial10format = new WritableCellFormat(arial10font); |
| 53 | + arial10format.setAlignment(jxl.format.Alignment.CENTRE); |
| 54 | + arial10format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); |
| 55 | + arial10format.setBackground(Colour.GRAY_25); |
| 56 | + |
| 57 | + arial12font = new WritableFont(WritableFont.ARIAL, 10); |
| 58 | + arial12format = new WritableCellFormat(arial12font); |
| 59 | + arial10format.setAlignment(jxl.format.Alignment.CENTRE);//对齐格式 |
| 60 | + arial12format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); //设置边框 |
| 61 | + |
| 62 | + } catch (WriteException e) { |
| 63 | + e.printStackTrace(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 初始化Excel |
| 69 | + * @param fileName |
| 70 | + * @param colName |
| 71 | + */ |
| 72 | + public static void initExcel(String fileName, String[] colName) { |
| 73 | + format(); |
| 74 | + WritableWorkbook workbook = null; |
| 75 | + try { |
| 76 | + File file = new File(fileName); |
| 77 | + if (!file.exists()) { |
| 78 | + file.createNewFile(); |
| 79 | + } |
| 80 | + workbook = Workbook.createWorkbook(file); |
| 81 | + WritableSheet sheet = workbook.createSheet("成绩表", 0); |
| 82 | + //创建标题栏 |
| 83 | + sheet.addCell((WritableCell) new Label(0, 0, fileName,arial14format)); |
| 84 | + for (int col = 0; col < colName.length; col++) { |
| 85 | + sheet.addCell(new Label(col, 0, colName[col], arial10format)); |
| 86 | + } |
| 87 | + sheet.setRowView(0,340); //设置行高 |
| 88 | + |
| 89 | + workbook.write(); |
| 90 | + } catch (Exception e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } finally { |
| 93 | + if (workbook != null) { |
| 94 | + try { |
| 95 | + workbook.close(); |
| 96 | + } catch (Exception e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @SuppressWarnings("unchecked") |
| 104 | + public static <T> void writeObjListToExcel(List<T> objList,String fileName, Context c) { |
| 105 | + if (objList != null && objList.size() > 0) { |
| 106 | + WritableWorkbook writebook = null; |
| 107 | + InputStream in = null; |
| 108 | + try { |
| 109 | + WorkbookSettings setEncode = new WorkbookSettings(); |
| 110 | + setEncode.setEncoding(UTF8_ENCODING); |
| 111 | + in = new FileInputStream(new File(fileName)); |
| 112 | + Workbook workbook = Workbook.getWorkbook(in); |
| 113 | + writebook = Workbook.createWorkbook(new File(fileName),workbook); |
| 114 | + WritableSheet sheet = writebook.getSheet(0); |
| 115 | + |
| 116 | +// sheet.mergeCells(0,1,0,objList.size()); //合并单元格 |
| 117 | +// sheet.mergeCells() |
| 118 | + |
| 119 | + for (int j = 0; j < objList.size(); j++) { |
| 120 | + ArrayList<String> list = (ArrayList<String>) objList.get(j); |
| 121 | + for (int i = 0; i < list.size(); i++) { |
| 122 | + sheet.addCell(new Label(i, j + 1, list.get(i),arial12format)); |
| 123 | + if (list.get(i).length() <= 5){ |
| 124 | + sheet.setColumnView(i,list.get(i).length()+8); //设置列宽 |
| 125 | + }else { |
| 126 | + sheet.setColumnView(i,list.get(i).length()+5); //设置列宽 |
| 127 | + } |
| 128 | + } |
| 129 | + sheet.setRowView(j+1,350); //设置行高 |
| 130 | + } |
| 131 | + |
| 132 | + writebook.write(); |
| 133 | + Toast.makeText(c, "导出到手机存储中文件夹Record成功", Toast.LENGTH_SHORT).show(); |
| 134 | + } catch (Exception e) { |
| 135 | + e.printStackTrace(); |
| 136 | + } finally { |
| 137 | + if (writebook != null) { |
| 138 | + try { |
| 139 | + writebook.close(); |
| 140 | + } catch (Exception e) { |
| 141 | + e.printStackTrace(); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + if (in != null) { |
| 146 | + try { |
| 147 | + in.close(); |
| 148 | + } catch (IOException e) { |
| 149 | + e.printStackTrace(); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + //----------------------------------读------------------------------------ |
| 158 | + |
| 159 | +// public static List<BillObject> read2DB(File f, Context con) { |
| 160 | +// ArrayList<BillObject> billList = new ArrayList<BillObject>(); |
| 161 | +// try { |
| 162 | +// Workbook course = null; |
| 163 | +// course = Workbook.getWorkbook(f); |
| 164 | +// Sheet sheet = course.getSheet(0); |
| 165 | +// |
| 166 | +// Cell cell = null; |
| 167 | +// for (int i = 1; i < sheet.getRows(); i++) { |
| 168 | +// BillObject tc = new BillObject(); |
| 169 | +// cell = sheet.getCell(1, i); |
| 170 | +// tc.setFood(cell.getContents()); |
| 171 | +// cell = sheet.getCell(2, i); |
| 172 | +// tc.setClothes(cell.getContents()); |
| 173 | +// cell = sheet.getCell(3, i); |
| 174 | +// tc.setHouse(cell.getContents()); |
| 175 | +// cell = sheet.getCell(4, i); |
| 176 | +// tc.setVehicle(cell.getContents()); |
| 177 | +// Log.d("gaolei", "Row"+i+"---------"+tc.getFood() + tc.getClothes() |
| 178 | +// + tc.getHouse() + tc.getVehicle()); |
| 179 | +// billList.add(tc); |
| 180 | +// |
| 181 | +// } |
| 182 | +// |
| 183 | +// } catch (Exception e) { |
| 184 | +// // TODO Auto-generated catch block |
| 185 | +// e.printStackTrace(); |
| 186 | +// } |
| 187 | +// return billList; |
| 188 | +// } |
| 189 | +// |
| 190 | +// public static Object getValueByRef(Class cls, String fieldName) { |
| 191 | +// Object value = null; |
| 192 | +// fieldName = fieldName.replaceFirst(fieldName.substring(0, 1), fieldName |
| 193 | +// .substring(0, 1).toUpperCase()); |
| 194 | +// String getMethodName = "get" + fieldName; |
| 195 | +// try { |
| 196 | +// Method method = cls.getMethod(getMethodName); |
| 197 | +// value = method.invoke(cls); |
| 198 | +// } catch (Exception e) { |
| 199 | +// e.printStackTrace(); |
| 200 | +// } |
| 201 | +// return value; |
| 202 | +// } |
| 203 | +} |
0 commit comments