| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- //
- //package test;
- //
- //import org.apache.poi.hssf.usermodel.HSSFCell;
- //import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- //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.ss.usermodel.*;
- //import org.apache.poi.xssf.streaming.SXSSFWorkbook;
- //
- //import com.nianzai.base.dao.util.DAOHelper;
- //import com.nianzai.base.mvc.Context;
- //import com.nianzai.base.mvc.util.ClassUtil;
- //import com.nianzai.base.mvc.util.Utils;
- //import com.nianzai.base.mvc.view.ExcelView;
- //import com.nianzai.base.mvc.view.View;
- //import com.nianzai.util.DateTimeUtil;
- //import com.nianzai.util.Tools;
- //
- //import org.apache.poi.xssf.streaming.SXSSFSheet;
- //import org.apache.poi.xssf.streaming.SXSSFRow;
- //import org.apache.poi.xssf.streaming.SXSSFCell;
- //import java.io.FileOutputStream;
- //import java.io.IOException;
- //import java.io.OutputStream;
- //import java.lang.reflect.Method;
- //import java.util.ArrayList;
- //import java.util.Date;
- //import java.util.List;
- //import java.util.Map;
- //import java.util.TreeMap;
- //
- //import javax.servlet.ServletException;
- //import javax.servlet.annotation.WebServlet;
- //import javax.servlet.http.HttpServlet;
- //import javax.servlet.http.HttpServletRequest;
- //import javax.servlet.http.HttpServletResponse;
- //
- //@WebServlet(
- // urlPatterns = {"/H.do"},
- // loadOnStartup = 0,
- // asyncSupported = true
- // )
- //public class H extends HttpServlet {
- //
- // protected Map<String, String> parseParameter(HttpServletRequest request) {
- // Map<String, String> result = new TreeMap();
- // Map<String, String[]> map = request.getParameterMap();
- //
- // for(Map.Entry<String, String[]> entry : map.entrySet()) {
- // String k = (String)entry.getKey();
- // String v = "";
- // if (((String[])entry.getValue()).length <= 1) {
- // v = ((String[])entry.getValue())[0];
- // } else {
- // String[] var11;
- // for(String s : var11 = (String[])entry.getValue()) {
- // v = v + s + ",";
- // }
- //
- // if (v.length() > 0) {
- // v = v.substring(0, v.length() - 1);
- // }
- // }
- //
- // v = Utils.parafilter(Utils.escapeDollarBackslash(v));
- // result.put(k, v);
- // }
- //
- // result.put("ip", Utils.getIpAddress(request));
- // if (request.getHeader("m") != null) {
- // result.put("_json", request.getHeader("m"));
- // }
- //
- // return result;
- // }
- //
- // protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // Map<String, String> paras = this.parseParameter(request);
- // Context context = new Context();
- // context.setParas(paras);
- // context.setRequest(request);
- // context.setResponse(response);
- // Map<String, Method> methodMap = ClassUtil.getMethodMap();
- // Map<String, Class<?>> classMap = ClassUtil.getClassMap();
- // if (methodMap == null) {
- // throw new RuntimeException("the methodMap is not inited");
- // } else {
- // String pathInfo = request.getRequestURI();
- // String contextPath = request.getContextPath();
- // if (!"".equals(contextPath)) {
- // pathInfo = pathInfo.replace(contextPath, "");
- // }
- //
- //
- // Method method = (Method)methodMap.get(pathInfo);
- //
- // try {
- //
- //
- // this.processExcel2(request, response);
- // } catch (Exception e) {
- // System.out.println(Tools.getExceptionMessage(e));
- // }
- // }
- //
- //
- // }
- //
- //
- // private void processExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
- //
- // List<Map<String, Object>> dataList = DAOHelper.find("device", "SELECT onum FROM `allorder`");
- // HSSFWorkbook wb = new HSSFWorkbook();
- // HSSFSheet sheet = wb.createSheet("导出数据");
- // HSSFCellStyle style = wb.createCellStyle();
- // style.setAlignment((short)2);
- //
- // for(int i = 0; i < dataList.size(); ++i) {
- // Map<String, Object> map = dataList.get(i);
- // Object[] obj = map.values().toArray();
- //
- // HSSFRow row = sheet.createRow(i);
- //
- // for(int j = 0; j < obj.length; ++j) {
- // HSSFCell cell = null;
- // cell = row.createCell(j, CellType.STRING);
- // if (!"".equals(obj[j]) && obj[j] != null) {
- // cell.setCellValue(obj[j].toString());
- // } else {
- // cell.setCellValue("");
- // }
- //
- // cell.setCellStyle(style);
- // sheet.autoSizeColumn(i);
- // }
- // }
- //
- // String fileName = "Excel-" + DateTimeUtil.longtime() + ".xls";
- // response.setContentType("application/vnd.ms-excel");
- // response.setHeader("Content-disposition", "attachment;filename=" + fileName);
- // OutputStream ouputStream = response.getOutputStream();
- // wb.write(ouputStream);
- // wb.close();
- // ouputStream.flush();
- // ouputStream.close();
- // }
- //
- //
- // private void processExcel2(HttpServletRequest request, HttpServletResponse response) throws IOException {
- //
- // List<Map<String, Object>> dataList = DAOHelper.find("device", "SELECT onum FROM `allorder`");
- // SXSSFWorkbook wb = new SXSSFWorkbook();
- // SXSSFSheet sheet = wb.createSheet("导出数据");
- // CellStyle style = wb.createCellStyle();
- // style.setAlignment((short)2);
- //
- //// sheet.trackAllColumnsForAutoSizing();
- //
- // for(int i = 0; i < dataList.size(); ++i) {
- // Map<String, Object> map = dataList.get(i);
- // Object[] obj = map.values().toArray();
- //
- // SXSSFRow row = sheet.createRow(i);
- //
- // for(int j = 0; j < obj.length; ++j) {
- // SXSSFCell cell = null;
- // cell = row.createCell(j, CellType.STRING);
- // if (!"".equals(obj[j]) && obj[j] != null) {
- // cell.setCellValue(obj[j].toString());
- // } else {
- // cell.setCellValue("");
- // }
- //
- // cell.setCellStyle(style);
- //// sheet.autoSizeColumn(i);
- // }
- // }
- //
- // String fileName = "Excel-" + DateTimeUtil.longtime() + ".xls";
- // response.setContentType("application/vnd.ms-excel");
- // response.setHeader("Content-disposition", "attachment;filename=" + fileName);
- // OutputStream ouputStream = response.getOutputStream();
- // wb.write(ouputStream);
- // wb.close();
- // ouputStream.flush();
- // ouputStream.close();
- // }
- //
- //
- //
- // public static void main(String[] args) {
- // try {
- // // 导出10万行测试数据
- // exportLargeExcel("e:\\deviceinbase\\export_data.xlsx", 100000);
- // System.out.println("Excel导出完成!");
- // } catch (IOException e) {
- // System.err.println("导出失败:" + e.getMessage());
- // e.printStackTrace();
- // }
- // }
- //
- // /**
- // * 使用SXSSFWorkbook导出大数据量Excel
- // */
- // public static void exportLargeExcel(String fileName, int rowCount) throws IOException {
- // long startTime = System.currentTimeMillis();
- //
- // // 创建SXSSFWorkbook,设置窗口大小为100行
- // // 内存中只保留100行数据,超出部分写入临时文件
- // HSSFWorkbook workbook = new HSSFWorkbook();
- //
- // // 可选:压缩临时文件以减少磁盘占用
- //// workbook.setCompressTempFiles(true);
- //
- // try {
- // // 创建工作表
- // HSSFSheet sheet = workbook.createSheet("大数据导出");
- //
- // // 创建表头
- // createHeaderRow(sheet);
- //
- // // 批量生成数据行
- // for (int i = 0; i < rowCount; i++) {
- // // 每1000行输出一次进度
- // if (i % 1000 == 0) {
- // System.out.println("正在生成第 " + i + " 行数据...");
- // }
- //
- // // 创建数据行
- // createDataRow(sheet, i + 1, i);
- // }
- //
- // // 自动调整列宽
- // autoSizeColumns(sheet, 5);
- //
- // // 写入文件
- // try (FileOutputStream out = new FileOutputStream(fileName)) {
- // workbook.write(out);
- // }
- //
- // long endTime = System.currentTimeMillis();
- // System.out.println("导出完成!总行数:" + rowCount +
- // ",耗时:" + (endTime - startTime) + "ms");
- //
- // } finally {
- // // 重要:清理临时文件
- //// workbook.dispose();
- // workbook.close();
- // }
- // }
- //
- // /**
- // * 创建表头行
- // */
- // private static void createHeaderRow(HSSFSheet sheet) {
- // HSSFRow headerRow = sheet.createRow(0);
- //
- // String[] headers = {"序号", "姓名", "年龄", "部门", "入职日期"};
- //
- // CellStyle headerStyle = createHeaderStyle(sheet.getWorkbook());
- //
- // for (int i = 0; i < headers.length; i++) {
- // HSSFCell cell = headerRow.createCell(i);
- // cell.setCellValue(headers[i]);
- // cell.setCellStyle(headerStyle);
- // }
- // }
- //
- // /**
- // * 创建数据行
- // */
- // private static void createDataRow(HSSFSheet sheet, int rowIndex, int dataIndex) {
- // HSSFRow row = sheet.createRow(rowIndex);
- //
- // // 序号
- // row.createCell(0).setCellValue(dataIndex + 1);
- //
- // // 姓名
- // row.createCell(1).setCellValue("员工" + (dataIndex + 1));
- //
- // // 年龄
- // row.createCell(2).setCellValue(20 + (dataIndex % 40));
- //
- // // 部门
- // String[] departments = {"技术部", "销售部", "人事部", "财务部", "市场部"};
- // row.createCell(3).setCellValue(departments[dataIndex % departments.length]);
- //
- // // 入职日期
- // row.createCell(4).setCellValue(new Date());
- // }
- //
- // /**
- // * 创建表头样式
- // */
- // private static CellStyle createHeaderStyle(Workbook workbook) {
- // CellStyle style = workbook.createCellStyle();
- //
- // // 设置背景色
- // style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
- // style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
- //
- // // 设置边框
- // style.setBorderBottom(BorderStyle.THIN);
- // style.setBorderTop(BorderStyle.THIN);
- // style.setBorderLeft(BorderStyle.THIN);
- // style.setBorderRight(BorderStyle.THIN);
- //
- // // 设置字体
- // Font font = workbook.createFont();
- // font.setBold(true);
- // style.setFont(font);
- //
- // return style;
- // }
- //
- // /**
- // * 自动调整列宽
- // */
- // private static void autoSizeColumns(HSSFSheet sheet, int columnCount) {
- // for (int i = 0; i < columnCount; i++) {
- //// sheet.trackColumnForAutoSizing(i);
- // sheet.autoSizeColumn(i);
- // }
- // }
- //}
|