欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 导出excle数据

程序员文章站 2022-03-07 22:31:19
...
/**
	 * @throws Exception
	 * 
	 * @Title: exportOrders @Description: TODO(批量导出) @param
	 *        
	 */
	@RequestMapping("/exportOrders")
	public void exportOrders(String[] fIds, HttpServletResponse reponse) throws Exception {
		List<ServiceOrderDto> list = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		map.put("array", fIds);
		list = orderService.queryProductListByIds(map);
		List<Object[]> dataList = new ArrayList<>();
		for (int i = list.size()-1; 0<= i; i--) {
			OrderBean o = list.get(i);
			Object[] obj = new Object[13];
			obj[0] = o.getfOrderCode();
			obj[1] = o.getfEntrepCode();
			if (o.getfEntrepName() != null) {
				obj[2] = o.getfEntrepName();
			} else {
				obj[2] = "--";
			}
			obj[3] = o.getfShopCode();
			obj[4] = o.getfUserAccount();
			obj[5] = "支付宝";
			obj[6] = o.getfPrice();
			obj[7] =0.00;
			obj[8] = o.getfPostFee();
			obj[9] = o.getfPrice() + o.getfPostFee();
			obj[10] = "一次性付款";
			String fState = "--";
			obj[11] = fState;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd  HH:mm:ss");
			obj[12] = sdf.format(o.getfCreateTime());
			dataList.add(obj);//导出数据
		}
		//列名称
		String[] columnName = { "订单编号", "卖家主体编号", "卖家主体名称", "卖家网站ID", "买家ID", "付款方式", "产品额(元)", "优惠额(元)", "运费额(元)", "交易额(元)", "付款类型",
				"订单状态", "创建日期" };
		Export.ExportWithResponse("订单数据", "订单数据", "订单数据", 13, null, columnName, dataList, reponse);

	}
package com.zuma.order.util;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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;
import org.apache.poi.ss.util.CellRangeAddress;

public class Export {
	 public static void ExportWithResponse(String sheetName, String titleName,  
	            String fileName, int columnNumber, int[] columnWidth,  
	            String[] columnName,List<Object[]> dataList,  
	            HttpServletResponse response) throws Exception {  
	       // if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {  
	            // 第一步,创建一个webbook,对应一个Excel文件  
	            HSSFWorkbook wb = new HSSFWorkbook();  
	            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
	            HSSFSheet sheet = wb.createSheet(sheetName);  
	             //sheet.setDefaultColumnWidth(15); //统一设置列宽  
	          /*  for (int i = 0; i < columnNumber; i++)   
	            {  
	                for (int j = 0; j <= i; j++)   
	                {  
	                    if (i == j)   
	                    {  
	                        sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽  
	                    }  
	                }  
	            } */ 
	            // 创建第0行 也就是标题  
	            HSSFRow row1 = sheet.createRow((int) 0);  
	            row1.setHeightInPoints(50);// 设备标题的高度  
	            // 第三步创建标题的单元格样式style2以及字体样式headerFont1  
	            HSSFCellStyle style2 = wb.createCellStyle();  
	            style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
	            style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
	            style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);  
	            style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
	            HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式  
	            headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
	            headerFont1.setFontName("黑体"); // 设置字体类型  
	            headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小  
	            style2.setFont(headerFont1); // 为标题样式设置字体样式  
	  
	            HSSFCell cell1 = row1.createCell(0);// 创建标题第一列  
	            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,  
	                    columnNumber - 1)); // 合并列标题  
	            cell1.setCellValue(titleName); // 设置值标题  
	            cell1.setCellStyle(style2); // 设置标题样式  
	  
	            // 创建第1行 也就是表头  
	            HSSFRow row = sheet.createRow((int) 1);  
	            row.setHeightInPoints(37);// 设置表头高度  
	  
	            // 第四步,创建表头单元格样式 以及表头的字体样式  
	            HSSFCellStyle style = wb.createCellStyle();  
	            style.setWrapText(true);// 设置自动换行  
	            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
	            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式  
	  
	            style.setBottomBorderColor(HSSFColor.BLACK.index);  
	            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
	            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
	            style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
	            style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
	  
	            HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式  
	            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗  
	            headerFont.setFontName("黑体"); // 设置字体类型  
	            headerFont.setFontHeightInPoints((short) 10); // 设置字体大小  
	            style.setFont(headerFont); // 为标题样式设置字体样式  
	  
	            // 第四.一步,创建表头的列  
	            for (int i = 0; i < columnNumber; i++)   
	            {  
	                HSSFCell cell = row.createCell(i);  
	                cell.setCellValue(columnName[i]);  
	                cell.setCellStyle(style);  
	            }  
	  
	            // 第五步,创建单元格,并设置值  
	            for (int i = 0; i < dataList.size(); i++)   
	            {  
	                row = sheet.createRow((int) i + 2);  
	                // 为数据内容设置特点新单元格样式1 自动换行 上下居中  
	                HSSFCellStyle zidonghuanhang = wb.createCellStyle();  
	                zidonghuanhang.setWrapText(true);// 设置自动换行  
	                zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式  
	  
	                // 设置边框  
	                zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);  
	                zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
	                zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
	                zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);  
	                zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);  
	  
	                // 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中  
	                HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();  
	                zidonghuanhang2.setWrapText(true);// 设置自动换行  
	                zidonghuanhang2  
	                        .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式  
	                zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
	  
	                // 设置边框  
	                zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);  
	                zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
	                zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
	                zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);  
	                zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);  
	                HSSFCell datacell = null;  
	                for (int j = 0; j < columnNumber; j++)   
	                {  
	                    datacell = row.createCell(j);  
	                    datacell.setCellValue((dataList.get(i))[j].toString());  
	                    datacell.setCellStyle(zidonghuanhang2);

	                }  
	            }
	            for(int j=0;j<columnNumber;j++){
   	             	sheet.autoSizeColumn(j);

	            }
	            sheet.setColumnWidth(11, 12*256);
	            sheet.setColumnWidth(12, 20*256);
	            // 第六步,将文件存到浏览器设置的下载位置  
	            String filename = fileName + ".xls";  
	            response.setContentType("application/ms-excel;charset=UTF-8");  
	            response.setHeader("Content-Disposition", "attachment;filename="  
	                    .concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));  
	            OutputStream out = response.getOutputStream();  
	            try {  
	                wb.write(out);// 将数据写出去  
	                String str = "导出" + fileName + "成功!";  
	                System.out.println(str);  
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                String str1 = "导出" + fileName + "失败!";  
	                System.out.println(str1);  
	            } finally {  
	                out.close();  
	            }  
	  
	     /*   } else {  
	            System.out.println("列数目长度名称三个数组长度要一致");  
	        } */ 
	  
	    }   
}
        <dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>3.14</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-excelant</artifactId>
		    <version>3.14</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>3.14</version>
		</dependency>
				<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-scratchpad</artifactId>
		    <version>3.14</version>
		</dependency>
				<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>ooxml-schemas</artifactId>
		    <version>1.1</version>
		</dependency>

需要引用的poi包

 

相关标签: excle poi