|
@@ -0,0 +1,186 @@
|
|
|
|
+package cn.com.qmth.print.manage.utils.excel;
|
|
|
|
+
|
|
|
|
+import org.apache.poi.xssf.usermodel.*;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Collection;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by dizhi on 2016/6/19.
|
|
|
|
+ */
|
|
|
|
+public class ExcelWriter extends ExcelUtils{
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public ExcelWriter(Class<?> dataClass){
|
|
|
|
+ super(dataClass,false);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 写入excel
|
|
|
|
+ * @param sheetName sheet名称
|
|
|
|
+ * @param dataset 数据集合
|
|
|
|
+ * @param out 输出流
|
|
|
|
+ */
|
|
|
|
+ public void write(String sheetName,Collection<?> dataset, OutputStream out) {
|
|
|
|
+ // 声明一个工作薄
|
|
|
|
+ XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
+ // 生成一个表格
|
|
|
|
+ XSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
|
+
|
|
|
|
+ // 设置表格默认列宽度为15个字节
|
|
|
|
+ sheet.setDefaultColumnWidth((short) 15);
|
|
|
|
+ // 生成一个样式
|
|
|
|
+ XSSFCellStyle style = workbook.createCellStyle();
|
|
|
|
+ // 设置这些样式
|
|
|
|
+// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
|
|
|
|
+// style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
|
|
|
|
+// style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style.setBorderRight(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style.setBorderTop(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
|
|
|
|
+ // 生成一个字体
|
|
|
|
+// XSSFFont font = workbook.createFont();
|
|
|
|
+// font.setColor(HSSFColor.VIOLET.index);
|
|
|
|
+// font.setFontHeightInPoints((short) 12);
|
|
|
|
+// font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
|
|
|
|
+// // 把字体应用到当前的样式
|
|
|
|
+// style.setFont(font);
|
|
|
|
+// // 生成并设置另一个样式
|
|
|
|
+// XSSFCellStyle style2 = workbook.createCellStyle();
|
|
|
|
+// style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
|
|
|
|
+// style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
|
|
|
|
+// style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style2.setBorderRight(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style2.setBorderTop(XSSFCellStyle.BORDER_THIN);
|
|
|
|
+// style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
|
|
|
|
+// style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
|
|
|
|
+// // 生成另一个字体
|
|
|
|
+// XSSFFont font2 = workbook.createFont();
|
|
|
|
+// font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
|
|
|
|
+ // 把字体应用到当前的样式
|
|
|
|
+ //style2.setFont(font2);
|
|
|
|
+
|
|
|
|
+ // 声明一个画图的顶级管理器
|
|
|
|
+ //HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
|
|
|
|
+ // 定义注释的大小和位置,详见文档
|
|
|
|
+// XSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
|
|
|
|
+// 0, 0, 0, (short) 4, 2, (short) 6, 5));
|
|
|
|
+// // 设置注释内容
|
|
|
|
+// comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
|
|
|
|
+// // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
|
|
|
|
+// comment.setAuthor("leno");
|
|
|
|
+
|
|
|
|
+ List<ColumnSetting> columnSettings = this.getColumnSettings();
|
|
|
|
+
|
|
|
|
+ // 产生表格标题行
|
|
|
|
+ XSSFRow row = sheet.createRow(0);
|
|
|
|
+ for (short i = 0; i < columnSettings.size(); i++) {
|
|
|
|
+ XSSFCell cell = row.createCell(i);
|
|
|
|
+ cell.setCellStyle(style);
|
|
|
|
+ XSSFRichTextString text = new XSSFRichTextString(columnSettings.get(i).getHeader());
|
|
|
|
+ cell.setCellValue(text);
|
|
|
|
+ if(columnSettings.get(i).getWidth() > 0){
|
|
|
|
+ sheet.setColumnWidth(i,columnSettings.get(i).getWidth()*256);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 遍历集合数据,产生数据行
|
|
|
|
+ //Iterator<?> it = dataset.iterator();
|
|
|
|
+ int index = 0;
|
|
|
|
+ for(Object obj : dataset) {
|
|
|
|
+ index++;
|
|
|
|
+ row = sheet.createRow(index);
|
|
|
|
+ //T t = (T) it.next();
|
|
|
|
+ // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
|
|
|
|
+ for (short i = 0; i < columnSettings.size(); i++) {
|
|
|
|
+ XSSFCell cell = row.createCell(i);
|
|
|
|
+ //cell.setCellStyle(style2);
|
|
|
|
+ String fieldName = columnSettings.get(i).getFieldName();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Field field=this.getDataClass().getDeclaredField(fieldName);
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
+ Object value = field.get(obj);
|
|
|
|
+ // 判断值的类型后进行强制类型转换
|
|
|
|
+ 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("yyyy-MM-dd");
|
|
|
|
+ textValue = sdf.format(date);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ // 其它数据类型都当作字符串简单处理
|
|
|
|
+ textValue = String.valueOf(value);
|
|
|
|
+ }
|
|
|
|
+ // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
|
|
|
|
+ if (textValue != null) {
|
|
|
|
+ Pattern p = Pattern.compile("^//d+(//.//d+)?$");
|
|
|
|
+ Matcher matcher = p.matcher(textValue);
|
|
|
|
+ if (matcher.matches()) {
|
|
|
|
+ // 是数字当作double处理
|
|
|
|
+ cell.setCellValue(Double.parseDouble(textValue));
|
|
|
|
+ } else {
|
|
|
|
+ XSSFRichTextString richString = new XSSFRichTextString(
|
|
|
|
+ textValue);
|
|
|
|
+
|
|
|
|
+ cell.setCellValue(richString);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (SecurityException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ // 清理资源
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ workbook.write(out);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ // TODO Auto-generated catch block
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|