Extjs4.0+HSSFWorkbook+SpringMVC实现将数据库中的记录导出到本地Excel格式

Extjs4.0+HSSFWorkbook+SpringMVC实现将数据库中的记录导出到本地Excel格式

自己封装的JS类Share.js

var Share = {};

Share.ExportByExcel = function(url) {

var appWindow = Share.getWin();

appWindow.open(url);

appWindow.focus();

};

调用的前端js

buttons : [{

text : ‘查询‘,

handler : querySettle

}/**, {

text : ‘转账‘,

handler : settleTransfer

}*/, {

text : ‘对账查询‘,

handler : settleQuery

}/**, {

text : ‘失败批量重转账‘,

handler : settleBatch

}*/, {

text : ‘重置查询条件‘,

handler : resetSettle

}, {

text : ‘导出Excel‘,

handler : function() {

var count = settleGrid.getStore().getCount();

var form = settleTopPanel.getForm();

var json = Ext.JSON.encode(form.getValues());

if (count > 0) {

var ids = getSelectedSettle();

if (ids == null) {

ids = "";

}

Share.ExportByExcel(ctx

+ "/exportSettleByExcel.htm?ids=" + ids

+ "&json=" + json);

} else {

Ext.MessageBox.alert("提示", ‘数据为空!‘);

}

}

}]

SpringMVC 的Controller类中

@RequestMapping("/exportSettleByExcel.htm")

public void exportSettleByExcel(@RequestParam String ids, @RequestParam String json, ModelMap map,HttpServletRequest request,

HttpServletResponse response) throws Exception {

logger.info("-------1------- exportSettleByExcel start!");

SettlementModel settlementModel = (SettlementModel) JSONCommonUtils.jsonToObject(json, SettlementModel.class);

List<SettleWaste> lisw = null;

if (settlementModel != null) {

String filename = "";

filename = "结算记录-" + settlementModel.getStartTime() + "到"

+ settlementModel.getEndTime();

settlementModel.setIds(ids);

lisw = settlementWasteService.querySettleWaste(settlementModel, ids);

if (lisw != null && lisw.size() > 0) {

HSSFWorkbook wb = SettleExcel.getHSSFWorkbook(lisw, filename);

PoiUtil.saveExcel(filename, request, response, wb);

}

}

}

SettleExcel.java封装的Excel定义类如下

package com.iboxpay.settlement.core.poi;

import java.util.List;

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.Font;

import com.iboxpay.settlement.core.constant.SettleConstants;

import com.iboxpay.settlement.core.domain.SettleWaste;

import com.iboxpay.settlement.util.CommonUtil;

import com.iboxpay.settlement.util.PoiUtil;

import com.iboxpay.settlement.util.TimeUtil;

public class SettleExcel {

public static HSSFWorkbook getHSSFWorkbook(List<SettleWaste> lisw, String title) {

HSSFWorkbook wb = new HSSFWorkbook(); // 创建工作簿

HSSFSheet sheet = wb.createSheet(); // 创建工作表

sheet.setDefaultColumnWidth(20); // 设置每列默认宽度

PoiUtil.createTitle(wb, sheet, title, 14); // 创建标题

HSSFRow row = sheet.createRow(1);

PoiUtil.createTitleCell(wb, row, 0, "实体账号");

PoiUtil.createTitleCell(wb, row, 1, "户名");

PoiUtil.createTitleCell(wb, row, 2, "交易金额(单位为:元)");

PoiUtil.createTitleCell(wb, row, 3, "手续费");

PoiUtil.createTitleCell(wb, row, 4, "应付金额(转账:单位元)");

PoiUtil.createTitleCell(wb, row, 5, "交易笔数");

PoiUtil.createTitleCell(wb, row, 6, "支付状态");

PoiUtil.createTitleCell(wb, row, 7, "网银支付行名");

PoiUtil.createTitleCell(wb, row, 8, "出账银行");

PoiUtil.createTitleCell(wb, row, 9, "结算类型");

PoiUtil.createTitleCell(wb, row, 10, "商户号");

PoiUtil.createTitleCell(wb, row, 11, "商户名称");

PoiUtil.createTitleCell(wb, row, 12, "出账日期");

PoiUtil.createTitleCell(wb, row, 13, "复核状态");

PoiUtil.createTitleCell(wb, row, 14, "交易日期");

PoiUtil.createTitleCell(wb, row, 15, "操作日期");

PoiUtil.createTitleCell(wb, row, 16, "操作员ID");

PoiUtil.createTitleCell(wb, row, 17, "民生指令ID");

PoiUtil.createTitleCell(wb, row, 18, "账号类型");

PoiUtil.createTitleCell(wb, row, 19, "备注");

PoiUtil.createTitleCell(wb, row, 20, "转账状态码");

PoiUtil.createTitleCell(wb, row, 21, "转账类型");

PoiUtil.createTitleCell(wb, row, 22, "汇路");

PoiUtil.createTitleCell(wb, row, 23, "网银支付行号");

PoiUtil.createTitleCell(wb, row, 24, "联行行号");

PoiUtil.createTitleCell(wb, row, 25, "联行行名");

Font font = wb.createFont();

font.setFontName("楷体");

HSSFCellStyle style = wb.createCellStyle();

style.setFont(font);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

if (lisw != null) {

int j = 2;

for (int i = 0; i < lisw.size(); i++) {

SettleWaste settleWaste = lisw.get(i);

row = sheet.createRow(j);

PoiUtil.createStringCell(wb,style, row, 0, settleWaste.getAccntNo());

PoiUtil.createStringCell(wb,style, row, 1, settleWaste.getAccntName());

PoiUtil.createDataCell(wb,style, row, 2, CommonUtil.FormatAmount(settleWaste.getTrnAmount()));

PoiUtil.createDataCell(wb,style, row, 3, CommonUtil.FormatAmount(settleWaste.getCharge()));

PoiUtil.createDataCell(wb,style, row, 4, CommonUtil.FormatAmount(settleWaste.getAccntAmount()));

PoiUtil.createDataCell(wb,style, row, 5, settleWaste.getTrncnt()!=null?settleWaste.getTrncnt():0.0);

if (SettleConstants.SETTLE_STATUS_UNPAY.equals(settleWaste.getXferFlag())) {

PoiUtil.createStringCell(wb,style, row, 6, "成功");

} else if (SettleConstants.SETTLE_STATUS_SUCCESS.equals(settleWaste.getXferFlag())) {

PoiUtil.createStringCell(wb,style, row, 6, "支付成功");

} else if(SettleConstants.SETTLE_STATUS_FAIL.equals(settleWaste.getXferFlag())) {

PoiUtil.createStringCell(wb,style, row, 6, "失败");

}

else if(SettleConstants.SETTLE_STATUS_NOT_SURE.equals(settleWaste.getXferFlag())){

PoiUtil.createStringCell(wb,style, row, 6, "支付未确定");

}

else {

PoiUtil.createStringCell(wb,style, row, 6, settleWaste.getXferFlag());

}

PoiUtil.createStringCell(wb,style, row, 7, settleWaste.getNetPayName());

PoiUtil.createStringCell(wb,style, row, 8, settleWaste.getBankName());

PoiUtil.createStringCell(wb,style, row, 9, "T+" + settleWaste.getStlexday());

PoiUtil.createStringCell(wb,style, row, 10, settleWaste.getClearMerchNo());

PoiUtil.createStringCell(wb,style, row, 11, settleWaste.getClearMerchant());

PoiUtil.createStringCell(wb,style, row, 12, settleWaste.getSettleDateFn());

if("0".equals(settleWaste.getChckFlag())){

PoiUtil.createStringCell(wb,style, row, 13, "未经办");

}

else if("1".equals(settleWaste.getChckFlag())){

PoiUtil.createStringCell(wb,style, row, 13, "经办通过");

}

else if("2".equals(settleWaste.getChckFlag())){

PoiUtil.createStringCell(wb,style, row, 13, "复核不通过");

}

else {

PoiUtil.createStringCell(wb,style, row, 13, settleWaste.getChckFlag());

}

PoiUtil.createStringCell(wb,style, row, 14, settleWaste.getSettleDate());

PoiUtil.createStringCell(wb,style, row, 15, TimeUtil.dateToString(settleWaste.getXferDate(), "yyyy-MM-dd HH:mm:ss"));

PoiUtil.createStringCell(wb,style, row, 16, settleWaste.getUserId());

PoiUtil.createStringCell(wb,style, row, 17, settleWaste.getInstId());

if("1".equals(settleWaste.getAccntType())){

PoiUtil.createStringCell(wb,style, row, 18, "对公");

}

else if("1".equals(settleWaste.getAccntType())){

PoiUtil.createStringCell(wb,style, row, 18, "对私");

}

else if("1".equals(settleWaste.getAccntType())){

PoiUtil.createStringCell(wb,style, row, 18, "对私存折");

}

else {

PoiUtil.createStringCell(wb,style, row, 18, settleWaste.getAccntType());

}

PoiUtil.createStringCell(wb,style, row, 19, settleWaste.getReserved());

PoiUtil.createStringCell(wb,style, row, 20, settleWaste.getRspCode());

if("1".equals(settleWaste.getXferType())){

PoiUtil.createStringCell(wb,style, row, 21, "跨行转账");

}

else if("2".equals(settleWaste.getXferType())){

PoiUtil.createStringCell(wb,style, row, 21, "同行转账");

}

else {

PoiUtil.createStringCell(wb,style, row, 21, settleWaste.getXferType());

}

if("0".equals(settleWaste.getLocalFlag())){

PoiUtil.createStringCell(wb,style, row, 22, "同行本地");

}

else if("1".equals(settleWaste.getLocalFlag())){

PoiUtil.createStringCell(wb,style, row, 22, "同行异地");

}

else if("2".equals(settleWaste.getLocalFlag())){

PoiUtil.createStringCell(wb,style, row, 22, "小额");

}

else if("3".equals(settleWaste.getLocalFlag())){

PoiUtil.createStringCell(wb,style, row, 22, "大额");

}

else if("4".equals(settleWaste.getLocalFlag())){

PoiUtil.createStringCell(wb,style, row, 22, "上海同城");

}

else if("5".equals(settleWaste.getLocalFlag())){

PoiUtil.createStringCell(wb,style, row, 22, "网银互联");

}

else {

PoiUtil.createStringCell(wb,style, row, 22, settleWaste.getLocalFlag());

}

PoiUtil.createStringCell(wb,style, row, 23, settleWaste.getNetPayNo());

PoiUtil.createStringCell(wb,style, row, 24, settleWaste.getUnionNo());

PoiUtil.createStringCell(wb,style, row, 25, settleWaste.getUnionName());

j++;

}

}

return wb;

}

}

POUtil.java类如下(将HSSFWorkbook封装后的对象写到客户端)

package com.iboxpay.settlement.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.text.DecimalFormat;

import java.util.HashMap;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

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.HSSFDataFormat;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;

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.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.CellStyle;

import org.apache.poi.ss.usermodel.DataFormat;

import org.apache.poi.ss.usermodel.Font;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class PoiUtil {

private static final Logger logger = LoggerFactory

.getLogger(PoiUtil.class);

private static HashMap<Integer, CellStyle> numberCellStyles = null;

private static CellStyle textCellStyle = null;

private static int DEFAULT_SHEET_INDEX = 0;

// 创建标题行

public static void createTitle(HSSFWorkbook wb, HSSFSheet sheet, String title, int margeCount) {

// HSSFRow titleRow = sheet.createRow(0); // 行和列索引都是从0开始

// HSSFCell titleCell = titleRow.createCell(0);

// // 设置样式

// HSSFCellStyle style = wb.createCellStyle();

// Font f = wb.createFont();

// f.setFontName("黑体"); // 字体

// f.setFontHeight((short) 300); // 字高

// style.setFont(f);

// style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置居中

// titleCell.setCellStyle(style); // 关联样式

// titleCell.setCellValue(title); // 列内容

// // 合并单元格

// sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, margeCount));

createTitle(wb, sheet, title, 0, 0, 0, margeCount);

}

// 创建标题行

public static void createTitle(HSSFWorkbook wb, HSSFSheet sheet, String title, int firstRow, int lastRow,

int firstCol, int margeCount) {

HSSFRow titleRow = sheet.createRow(firstRow); // 行和列索引都是从0开始

HSSFCell titleCell = titleRow.createCell(firstCol);

// 设置样式

HSSFCellStyle style = wb.createCellStyle();

Font f = wb.createFont();

f.setFontName("黑体"); // 字体

f.setFontHeight((short) 300); // 字高

style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置居中

titleCell.setCellStyle(style); // 关联样式

titleCell.setCellValue(title); // 列内容

// 合并单元格

sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, margeCount));

}

// 创建标题行

public static void createSecondTitle(HSSFWorkbook wb, HSSFSheet sheet, String title, int firstRow, int lastRow,

int firstCol, int margeCount) {

HSSFRow titleRow = sheet.createRow(firstRow); // 行和列索引都是从0开始

HSSFCell titleCell = titleRow.createCell(firstCol);

// 设置样式

HSSFCellStyle style = wb.createCellStyle();

Font f = wb.createFont();

f.setFontName("楷体");

style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 设置居中

titleCell.setCellStyle(style); // 关联样式

titleCell.setCellValue(title); // 列内容

// 合并单元格

sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, margeCount));

}

// 创建列

public static void createTitleCell(HSSFWorkbook wb, HSSFRow row, int column, String cellValue) {

HSSFCell cell = row.createCell(column);

HSSFCellStyle style = wb.createCellStyle();

Font f = wb.createFont();

// if (row.getRowNum() == 1) {

// f.setFontName("宋体");

// f.setFontHeight((short) 260);

// f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 设置粗体

// } else {

f.setFontName("楷体");

// }

style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置居中

cell.setCellStyle(style);

cell.setCellValue(cellValue);

}

public static void createTitleCell(HSSFWorkbook wb,HSSFCellStyle style, HSSFRow row, int column, String cellValue) {

HSSFCell cell = row.createCell(column);

cell.setCellStyle(style);

cell.setCellValue(cellValue);

}

// 创建字符串列

public static void createStringCell(HSSFWorkbook wb, HSSFRow row, int column, String cellValue) {

HSSFCell cell = row.createCell(column);

HSSFCellStyle style = wb.createCellStyle();

Font f = wb.createFont();

f.setFontName("楷体");

style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

cell.setCellValue(cellValue);

}

public static void createStringCell(HSSFWorkbook wb,HSSFCellStyle style, HSSFRow row, int column, String cellValue) {

HSSFCell cell = row.createCell(column);

cell.setCellStyle(style);

cell.setCellValue(cellValue);

}

// 创建数字列

public static void createDataCell(HSSFWorkbook wb, HSSFRow row, int column, int cellValue) {

HSSFCell cell = row.createCell(column);

HSSFCellStyle style = wb.createCellStyle();

Font f = wb.createFont();

f.setFontName("楷体");

style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

cell.setCellValue((double) cellValue);

}

public static void createDataCell(HSSFWorkbook wb,HSSFCellStyle style, HSSFRow row, int column, int cellValue) {

HSSFCell cell = row.createCell(column);

//        HSSFCellStyle style = wb.createCellStyle();

//        Font f = wb.createFont();

//

//        f.setFontName("楷体");

//        style.setFont(f);

//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

cell.setCellValue((double) cellValue);

}

// 创建数字列

public static void createDataCell(HSSFWorkbook wb, HSSFRow row, int column, String cellValue) {

HSSFCell cell = row.createCell(column);

HSSFCellStyle style = wb.createCellStyle();

Font f = wb.createFont();

f.setFontName("楷体");

style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

HSSFDataFormat format = wb.createDataFormat();

style.setDataFormat(format.getFormat("@"));

cell.setCellStyle(style);

// double d = Double.valueOf(cellValue);

cell.setCellValue(new HSSFRichTextString(cellValue));

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

}

public static void createDataCell(HSSFWorkbook wb,HSSFCellStyle style, HSSFRow row, int column, String cellValue) {

HSSFCell cell = row.createCell(column);

//        HSSFCellStyle style = wb.createCellStyle();

//        Font f = wb.createFont();

//        f.setFontName("楷体");

//        style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

HSSFDataFormat format = wb.createDataFormat();

style.setDataFormat(format.getFormat("@"));

cell.setCellStyle(style);

// double d = Double.valueOf(cellValue);

cell.setCellValue(new HSSFRichTextString(cellValue));

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

}

// 创建数字列

public static void createDataCell(HSSFWorkbook wb, HSSFRow row, int column, double cellValue) {

HSSFCell cell = row.createCell(column);

HSSFCellStyle style = wb.createCellStyle();

Font f = wb.createFont();

f.setFontName("楷体");

style.setFont(f);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

double d = Double.valueOf(cellValue);

cell.setCellValue(d);

}

public static void createDataCell(HSSFWorkbook wb,HSSFCellStyle style, HSSFRow row, int column, double cellValue) {

HSSFCell cell = row.createCell(column);

//        HSSFCellStyle style = wb.createCellStyle();

//        Font f = wb.createFont();

//        f.setFontName("楷体");

//        style.setFont(f);

//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cell.setCellStyle(style);

double d = Double.valueOf(cellValue);

cell.setCellValue(d);

}

public static void writeNumber(Workbook workBook, int sheetIndex, int rowIndex, int colIndex, double value,

int scale) {

Cell cell = getCell(workBook, sheetIndex, rowIndex, colIndex);

HSSFCellStyle cellStyle = (HSSFCellStyle) getNumberCellStyle(workBook, scale);

cell.setCellStyle(cellStyle);

cell.setCellValue(value);

}

public void writeText(Workbook workBook, int sheetIndex, int rowIndex, int colIndex, String value) {

Cell cell = getCell(workBook, sheetIndex, rowIndex, colIndex);

HSSFCellStyle cellStyle = (HSSFCellStyle) getTextCellStyle(workBook);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));

cell.setCellStyle(cellStyle);

cell.setCellValue(new HSSFRichTextString(value));

}

public void writeText(Workbook workBook, int rowIndex, int colIndex, String value) {

writeText(workBook, DEFAULT_SHEET_INDEX, rowIndex, colIndex, value);

}

public void writeTextByNo(Workbook workBook, int rowIndex, int colIndex, String value) {

writeText(workBook, DEFAULT_SHEET_INDEX, rowIndex - 1, colIndex - 1, value);

}

public void writeNumber(Workbook workBook, int rowIndex, int colIndex, int value, int scale) {

writeNumber(workBook, DEFAULT_SHEET_INDEX, rowIndex, colIndex, value, scale);

}

public void writeNumberByNo(Workbook workBook, int rowIndex, int colIndex, int value, int scale) {

writeNumber(workBook, DEFAULT_SHEET_INDEX, rowIndex - 1, colIndex - 1, value, scale);

}

private static Cell getCell(Workbook workBook, int sheetIndex, int rowIndex, int colIndex) {

// Sheet

Sheet sheet = null;

try {

sheet = workBook.getSheetAt(sheetIndex);

} catch (IllegalArgumentException ex) {

sheet = workBook.createSheet();

}

// Row

Row row = null;

row = sheet.getRow(rowIndex);

if (row == null) {

row = sheet.createRow(rowIndex);

}

// Cell

Cell cell = null;

cell = row.getCell(colIndex);

if (cell == null) {

cell = row.createCell(colIndex);

}

return cell;

}

private static CellStyle getNumberCellStyle(Workbook workBook, int scale) {

if (numberCellStyles == null) {

numberCellStyles = new HashMap<Integer, CellStyle>();

}

if (numberCellStyles.get(Integer.valueOf(scale)) == null) {

CellStyle numberCellStyle = workBook.createCellStyle();

StringBuilder zeroBd = new StringBuilder();

DataFormat format = workBook.createDataFormat();

zeroBd.append("0");

if (scale > 0) {

zeroBd.append(".");

for (int zCount = 0; zCount < scale; zCount++) {

zeroBd.append("0");

}

}

short doubleFormat = format.getFormat(zeroBd.toString());

numberCellStyle.setDataFormat(doubleFormat);

numberCellStyles.put(Integer.valueOf(scale), numberCellStyle);

return numberCellStyle;

} else {

return numberCellStyles.get(Integer.valueOf(scale));

}

}

private static CellStyle getTextCellStyle(Workbook workBook) {

if (textCellStyle != null) {

return textCellStyle;

} else {

return workBook.createCellStyle();

}

}

public static Workbook getWorkbook(File file) throws FileNotFoundException, IOException {

Workbook workBook = null;

try {

// 读取office 2007版本以上

workBook = new XSSFWorkbook(new FileInputStream(file));

} catch (Exception ex) {

// 读取office 2003版本以下

workBook = new HSSFWorkbook(new FileInputStream(file));

}

return workBook;

}

public static void saveExcel(String filename, HttpServletRequest request, HttpServletResponse response,

HSSFWorkbook wb) {

ServletOutputStream out = null;

try {

out = response.getOutputStream();

// response.setHeader("Content-Disposition", "inline; filename="

// + URLEncoder.encode(filename, "UTF-8"));

// response.setCharacterEncoding("UTF-8");

// response.setContentType("application/vnd.ms-excel");

if (request.getHeader("user-agent").indexOf("MSIE") != -1) {

filename = java.net.URLEncoder.encode(filename, "utf-8") + ".xls";

} else {

filename = new String(filename.getBytes("utf-8"), "iso-8859-1") + ".xls";

}

response.setHeader("Content-Disposition", "attachment;filename=" + filename);

response.setContentType("application/msexcel");

wb.write(out);

out.flush();// 清除缓存

out.close();

} catch (Exception e) {

logger.error("Eeception:saveExcel");

}

}

public static void saveExcel(Workbook wb,String filename, String version, HttpServletRequest request, HttpServletResponse response) {

ServletOutputStream out = null;

try {

out = response.getOutputStream();

// response.setHeader("Content-Disposition", "inline; filename="

// + URLEncoder.encode(filename, "UTF-8"));

// response.setCharacterEncoding("UTF-8");

// response.setContentType("application/vnd.ms-excel");

//            if(Constants.EXCEL_2007.equals(version)){

//                filename = filename + ".xlsx";

//            }

//            else{

//                filename = filename + ".xls";

//            }

//

//            if (request.getHeader("user-agent").indexOf("MSIE") != -1) {

//                filename = java.net.URLEncoder.encode(filename, "utf-8");// + ".xls";

//            } else {

//                filename = new String(filename.getBytes("utf-8"), "iso-8859-1");// + ".xls";

//            }

//

//            response.setHeader("Content-Disposition", "attachment;filename=" + filename);

//            response.setContentType("application/msexcel");

//

//            wb.write(out);

//            out.flush();// 清除缓存

//            out.close();

} catch (Exception e) {

logger.error("Eeception:saveExcel");

}

}

public static String readExcelContent(InputStream is) {

POIFSFileSystem fs = null;

XSSFWorkbook wb = null;

XSSFSheet sheet = null;

XSSFRow row = null;

String str = "";

try {

//            fs = new POIFSFileSystem(is);

wb = new XSSFWorkbook(is);

} catch (IOException e) {

e.printStackTrace();

}

sheet = wb.getSheetAt(0);

// 得到总行数

int rowNum = sheet.getLastRowNum();

row = sheet.getRow(0);

int colNum = row.getPhysicalNumberOfCells();

// 正文内容应该从第二行开始,第一行为表头的标题

for (int i = 1; i <= rowNum; i++) {

row = sheet.getRow(i);

int j = 0;

// while (j < colNum) {

// // 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据

// // 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean

// // str += getStringCellValue(row.getCell((short) j)).trim() +

// // "-";

// str += getStringCellValue(row.getCell((short) j)).trim() + "";

// j++;

// }

// content.put(i, str);

// str = "";

// double d = row.getCell(0).getNumericCellValue();

// System.out.println("double=" + d);

DecimalFormat df = new DecimalFormat("#");// 转换成整型

//            if (i == rowNum) {

//                str += df.format(row.getCell(0).getNumericCellValue());

//            } else {

//                str += df.format(row.getCell(0).getNumericCellValue()) + ",";

//            }

System.out.println(df.format(row.getCell(2).getNumericCellValue()));

if(i == 100){

break;

}

}

return str;

}

}

时间: 2024-11-05 18:43:18

Extjs4.0+HSSFWorkbook+SpringMVC实现将数据库中的记录导出到本地Excel格式的相关文章

转换 数据库中日期(/Date(1351699200000)/) 的格式

转换 数据库中日期(/Date(1351699200000)/) 的格式: C#中转换日期格式 var date=com.CREATEDATETIME.ToString(); JavaScript中转换日期格式 //转日期: function getDateStr(dateDB) { //传数据库中日期 var numStr = dateDB.replace(/[^0-9]/g, ''); var num = parseInt(numStr); var strDate = new Date(nu

查询数据库中重复记录的方法

select * from [DataBase].[dbo].[TableName] where [字段一] in (select [字段一] from [DataBase].[dbo].[TableName] group by [字段一] having count([字段一]) > 1) 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from peop

有关在数据库中的 记录用户的登录状态

当用户登录的时候,判断该用户是否等录了.用于处理 不让同一个账号在不同的时间,不同的IP上 进行重复登录 所进行的操作! 每当用户登录的时候,就记录用户的登录日志(用户的ID,登录时间,退出时间,IP)等. 当你进行登录的时候,用户的登录状态为1,退出的时候 用户的登录状态为0, 当用户进行正常的登录.退出的时候,没有问题,用户登录日志也填上了. 但是要是用户非正常的退出呢? 那么用户登录的时候,这时数据库中 用户的登录状态 仍为1 ,那么用户就没有办法登录了. 故 我搜索了几天的数据,终于找到

jquery 解析数据库中的json日期为正常的格式

//在action从后台数据库中请求获得日期以后,得到的是json格式的数据,因此要解析才能显示在前台1.在jsp页面写的代码如下:<html> <script> Date.prototype.format = function(format) { var o = { "M+": this.getMonth() + 1, // month "d+": this.getDate(), // day "h+": this.ge

在ASP.NET Web Forms中使用页面导出伪xls Excel表格

将数据导出为Excel表格是比较常见的需求,也有很多组件支持导出真正的Excel表格.由于Excel能打开HTML文件,并支持其中的table元素以及p之类的文本元素的显示,所以把.html扩展名改为.xls是比较常用的一种方式.当然这只是一种骗人的伎俩,所以我称之为伪xls表格.不过对于要求不高的需求,这种方法还是比较简单快捷的. 在Web Forms项目中,除了用代码拼凑HTML元素标记外,还可以直接用窗体页面渲染HTML表格.Web Forms就是用来渲染动态HTML的,直接利用它,支持代

在Java中导出word、excel格式文件时JSP页面头的设置

我们在JSP中往往会把一些表格里的东西需要导出到本地,一般都是导成word.excel格式的文件.这只需要在JSP页面头设置及在<head></head>标签中添加下面的代码: 导出为excel: <% response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition","atta

zabbix 3.0 编译安装到 oracle 数据库中 &nbsp; centos 6.6

基于centos 6.6  编译安装zabbix 3.0  数据存储到oracle中 因为服务器多了以后, 依赖mysql为后台的zabbix 性能明显会下降.那么我们将其安装到oracle中,以下是安装文档. 基础 : zabbix3.0.1 版本, php5.5     centos 6.6   oracle 11.204 安装httpd yum install httpd. 1. 要用 php55. https://oss.oracle.com/projects/php/files/EL6

Oracle 数据库中对记录进行分页处理——学习笔记

学习到 oracle 的视图的时候,了解到对 Oracle 中数据的记录进行分页处理和 Mysql 提供的 limit 来进行分页处理大有不同,limit 是 mysql 中特有的关键字. 那么在 oracle 中如何实现对记录的分页处理呢? 解决办法:使用 ROWNUM 1.问题如下:查询出成绩前10名的学生的成绩 SELECT name, scoreFROM (SELECT name,score FROM students   ORDER BY score DESC )WHERE rownu

oracle 数据库中 date类型数据查询操作,格式转换,字符转date

//查询日期(类型为date)的数据 select * from auth_organization_t t where to_char(create_date,'yyyy-mm-dd hh:mi:ss') = '2013-08-12 05:31:09' select to_char(create_date,'yyyy-mm-dd hh:mi:ss') from auth_organization_t t