POI 生成表格通用方法
2018-01-24 00:00
127 查看
import java.util.Collection;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ExcelExportServiceImpl implements ExcelExportService {
}
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static String getName = "get";
}
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ExcelExportServiceImpl implements ExcelExportService {
@Autowired HttpServletResponse response; [@Override](https://my.oschina.net/u/1162528) public <T> void excelData(String title, String[] headers, String[] colums, Collection<T> dataset) throws Exception { ExcelUtils.exportExcelData(title, headers, colums, dataset,response); response.setCharacterEncoding("UTF-8"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
}
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static String getName = "get";
private static String dataFormat = "yyyy-MM-dd"; public static <T> void exportExcelData(String title, String[] headers, String[] colums, Collection<T> dataset,HttpServletResponse response) throws Exception { XSSFWorkbook workBook = null; try { // 创建一个workbook 对应一个excel应用文件 workBook = new XSSFWorkbook(); // 在workbook中添加一个sheet,对应Excel文件中的sheet XSSFSheet sheet = workBook.createSheet(title); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth((short) 15); // 构建表头 XSSFRow headRow = sheet.createRow(0); XSSFCell cell = null; for (int i = 0; i < headers.length; i++) { cell = headRow.createCell(i); XSSFRichTextString text = new XSSFRichTextString(headers[i]); cell.setCellValue(text); } // 构建表体数据 Iterator<T> it = dataset.iterator(); int index = 0; while (it.hasNext()) { index++; XSSFRow bodyRow = sheet.createRow(index); T t = (T) it.next(); for (int i = 0; i < colums.length; i++) { cell = bodyRow.createCell(i); String fieldName = colums[i]; String getMethodName = getName + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Class<? extends Object> tCls = t.getClass(); Method getMethod = tCls.getMethod(getMethodName, new Class[] {}); Object value = getMethod.invoke(t, new Object[] {}); String textValue = null; if (value instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat(dataFormat); textValue = sdf.format(date); } else if (value != null) { // 其它数据类型都当作字符串简单处理 textValue = value.toString(); } else { textValue = " "; } cell.setCellValue(textValue); } } workBook.write(response.getOutputStream()); } finally { if (workBook != null) { workBook.close(); } } }
}
相关文章推荐
- java中用poi处理Excel表格之写入(2003和2007通用方法)
- java中用poi处理Excel表格之读取(2003和2007通用方法)
- 表格添加、删除行通用方法
- asp.net下用Aspose.Words for .NET动态生成word文档中的数据表格的方法
- poi读取Excel时日期为数字 的解决方法 转载 2017年12月14日 10:31:14 标签:poi /excel /java 19 在使用poi解析Excel文件时,会发现,表格中的日期解析出
- asp.net 生成Excle 和 word 通用方法
- 软件测试中通用数据生成方法
- php自定义方法:自动生成数据表格(支持分页)
- 在easyui框架中使用JS和window.open方法提交数据至Servlet并导出POI数据表格
- 生成一像素表格边框的五种方法
- jQuery实现动态生成表格并为行绑定单击变色动作的方法
- update通用生成SQL语句方法
- poi生成excel表格数据和解析excel表格数据
- 实现poi方式生成excel文件和web类根据路径生成java实例的反射技术的springmvc方法的方法
- 简单OA项目笔记(5):生成、下载excel格式的动态表格(POI)
- JS控制网页动态生成任意行列数表格的方法
- ASP.net中动态生成表格/文本框以及获取文本框内容的方法
- C#生成图片缩略图、水印及上传的几个通用方法
- 使用itext根据freemarks模版生成pdf,表格产生跨页问题解决方法