vue项目接口域名动态的获取方法
216
2023-07-11
Java程序实现导出Excel的方法(支持IE低版本)
今天想整理一下自己前段时间遇到的一个导出的问题。
因为项目的需求,要做一部分导出功能。开始的时候用的公司的导出,但是很奇怪有部分模块导出的时候就是会报500错误,发现在删减一些字段后就恢复了正常,当时因为项目紧张,也就临时删减了一些,但也不是长久之计,之后自己在原本的基础上重新修改整理了一下,目前运行还算稳定,就此和大家分享一下。
导出需要三个部分,js,公共方法,后台方法。
js代码
function exportData()
{
//前台接收的参数
rwmc = $("#txt_rwmc").val();
rwlb = $("#com_rwlb").combobox("getValues").join(",");
//调用后台导出功能
var service = new Service("cx.RybjcxBndService.exprotExcel");
var str="
var res = service.doService(str);
var oDoc = loadXml(res);
if (service.getCode() != "2000") {
showMessage("查询失败:"+service.getMessage());
return;
}
var nodata = oDoc.selectSingleNode("ROOT/NODATA").text;
if (nodata == "nodata") {
showMessage("无数据!");
return;
}
// 获取导出信息
var titleName = oDoc.selectSingleNode("ROOT/TITLE_NAME").text;
var fileName = oDoc.selectSingleNode("ROOT/FILE_NAME").text;
var outPutInfo = oDoc.selectSingleNode("ROOT/OUTPUTINFO").text;
var download_path = oDoc.selectSingleNode("ROOT/DOWNLOAD_PATH").text;
if (outPutInfo != "") {
showMessage(outPutInfo);
return;
}
if (confirm("导出成功!确认下载文件吗?\n文件名称为:"+fileName)) {
var file = fileName;
var showfile = titleName + ".xls";
showfile = decodeURIComponent(showfile);
var idx = document.URL.indexOf("/adp");
if (idx == -1) {
alert("无法识别主机地址:" + document.URL);
return;
}
var host = document.URL.substring(0, idx);
var width = screen.width;
var height = screen.height;
debugger;
// 打开下载页面
var param = "toolbar=no,location=no,status=yes,resizable=no,scrollbars=yes,top=" + height + ",left=" + width + ",width=100,height=100";
// ----------------------------------------------------------
// 此代码块为解决ie6下导出excel失败问题,原因是ie6对window.open(url)支持度不好,
// 当浏览器为ie6时改用window.location.href
var isIE=!!window.ActiveXObject;
var isIE6=isIE&&!window.XMLHttpRequest;
if (isIE6) {
window.location.href=host + "/adp/work/gzkp/common/js/download_new.jsp?file=" + file + "&showfile=" + showfile + "&download_path=" + download_path;
} else {
window.open(host + "/adp/work/gzkp/common/js/download_new.jsp?file=" + file + "&showfile=" + showfile + "&download_path=" + download_path, "_blank", param);
}
}
}
公共类
package ctais.business.gzkp.common;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi2.hssf.usermodel.HSSFCell;
import org.apache.poi2.hssf.usermodel.HSSFCellStyle;
import org.apache.poi2.hssf.usermodel.HSSFFont;
import org.apache.poi2.hssf.usermodel.HSSFRow;
import org.apache.poi2.hssf.usermodel.HSSFSheet;
import org.apache.poi2.hssf.usermodel.HSSFWorkbook;
import ctais.business.dashboard.service.ExportExcel;
import ctais.config.Config;
import ctais.services.data.DataWindow;
import ctais.services.xml.XMLDataObject;
import ctais.services.xml.XMLParser;
import ctais.util.StringEx;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
*
Title: 生成EXCEL文件
*
Description: 转换String字符串为EXCEL文档
*
Copyright: Copyright (c) 2004
*
Company: DC
* @author FENGZG
* @version 1.0
* 时间:2015-12-28
*/
public class CreateExcel {
private final static String CONFIG_FILE_PATH = Config.CTAIS_HOME;
WritableWorkbook wwb = null;
XMLDataObject xdo = null;
public CreateExcel(){
}
/**
* 生成EXCEL
* @param sql 查询SQL
* @param czryDm 操作人员代码
* @param titles 导出列标题
* @param exlTitle excel表头
* @return
* @throws Exception
*/
public String newToExcel(String sql,String czryDm,String[] titles,String exlTitle) throws Exception
{
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
ExportExcel exportExcel = new ExportExcel(wb, sheet);
StringBuffer sffer = new StringBuffer();
//int colNum = 30;
DataWindow dw = DataWindow.dynamicCreate(sql.toString());
dw.setConnectionName(Icomm.GZKPJNDI);
long dwRet = dw.retrieve();
if (dwRet <= 0) {
sffer.append("
return sffer.toString();
} else {
sffer.append("
}
int colNum = dw.getColumnCount();
// 给工作表列定义列宽(实际应用自己更改列数)
for (short i = 0; i <= colNum; i++) {
sheet.setColumnWidth(i, (short) 4000);
}
// 创建单元格样式
HSSFCellStyle cellHeadStyle = wb.createCellStyle();
// 指定单元格居中对齐
cellHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 指定单元格垂直居中对齐
cellHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 指定当单元格内容显示不下时自动换行
cellHeadStyle.setWrapText(true);
// 设置单元格字体
HSSFFont headFont = wb.createFont();
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headFont.setFontName("宋体");
headFont.setFontHeight((short) 200);
cellHeadStyle.setFont(headFont);
// 创建报表头部
Date dt=new Date();
SimpleDateFormat sdt=new SimpleDateFormat("yyyyMMddhhmmssS");
String sfm = czryDm + "_" + sdt.format(dt);
// 设置列头
exportExcel.createNormalHead(exlTitle, colNum-1);
HSSFRow row1 = sheet.createRow(1);
for(int i = 0; i < titles.length; i ++) {
HSSFCell cell = row1.createCell((short)i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellHeadStyle);
cell.setCellValue(titles[i]);
}
Object value = "";
//设置表格样式
HSSFCellStyle cellStyle = wb.createCellStyle();
// 指定单元格居中对齐
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 指定单元格垂直居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 指定当单元格内容显示不下时自动换行
cellStyle.setWrapText(true);
// 设置单元格字体
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.SS_NONE);
http:// font.setFontName("宋体");
font.setFontHeight((short) 200);
cellStyle.setFont(font);
for(int i = 0 ; i < dw.getRowCount(); i++) {
HSSFRow row = sheet.createRow(i + 2);
for(int j = 1; j <= dw.getColumnCount(); j++) {
HSSFCell cell = row.createCell((short)(j-1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
value = dw.getItemAny(i, j-1);
if(value == null) {
cell.setCellValue("");
} else {
cell.setCellValue(value.toString());
}
}
}
//设置导出路径,此处需要注意如果是linux系统需要手动建路径,(此处的原因有人比较清楚的话还请指教)引用新建的文件路径
String path = "/export/";
File file = new File(path);
if(!file.exists()) {
file.mkdirs();
}
String fileName = sfm+".xls";
//String pth = path.trim() + File.separator + fileName;
String pth = path.trim() + fileName;
pth = pth.trim();
String outPutInfo = exportExcel.outputExcel(pth);
sffer.append("
sffer.append("
sffer.append("
sffer.append("
return sffer.toString();
}
catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
}
后台代码
/**
* 导出功能
* @param xdo 前台传参
* @param czryDm 操作人员代码
* @return 生成的XLS信息
* @throws Exception 异常说明
*/
public String exportExcel(XMLDataObject args,String czryDm) throws Exception
{
//接收前台传递的查询参数
String rwmc = StringEx.sNull(args.getItemValue("RWMC"));
String rwlb = StringEx.sNull(args.getItemValue("RWLB"));
if(null != rwmc && !"".equals(rwmc))
{
sqlWhere.append(" AND A.RWMC LIKE '%"+rwmc+"%' ");
}
if(null != rwlb && !"".equals(rwlb))
{
sqlWhere.append(" AND A.RWLB_DM = '"+rwlb+"' ");
}
StringBuilder sql = new StringBuilder();
//拼接查询SQL
sql.append("SELECT RWXH,RWMC FROM RWXX")
.append(sqlWhere).append(" ORDER BY RWXH ) ").append(sqlisWhere);
//导出的列标题
String[] titles = {"任务序号","任务名称"};
//实例化公共类
CreateExcel excel = new CreateExcel();
return excel.newToExcel(sql.toString(), czryDm,titles,"Exlcel表头");
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~