web环境;spring+jQuery+hibernate
前端jsp文件核心代码:
前后端交互配置文件:
nsl-servlet.xml里配置网页和后台交互的一一对应:
"http://www.springframework.org/dtd/spring-beans.dtd">
updatepassController
需要的jar包:ooxml-schemas-1.1.jar,poi-ooxml-3.15.jar,poi-3.15.jar,poi-examples-3.15.jar,poi-excelant-3.15.jar,poi-scratchpad-3.15.jar
后台ajax下载文件:
package cn.nsl.web.embed.ajax;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import cn.nsl.domain.glob.*;
import cn.nsl.web.util.Md5Security;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jbetter.spring.web.JSONObjectAbstractController;
public class UpdatepassController extends JSONObjectAbstractController {
@Override
protected String handleAjaxRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String userid = request.getParameter("userid");
String json ="{}";
Gson gson = new GsonBuilder().create(); //创建json对象,目的将最后想要的数据传递到前台
String subtype = request.getParameter("subtype"); //下载类型
HashMap
if("dwcxqd".equals(subtype)){
String attid = request.getParameter("attid");
XWPFDocument document= new XWPFDocument();
FileSystemView fsv = FileSystemView.getFileSystemView();
File com=fsv.getHomeDirectory(); //获取桌面路径
List
FileOutputStream outs = new FileOutputStream(new File(com+"/cxdd.doc")); //在本地桌面生成word文件
XWPFParagraph titleParagraph = document.createParagraph(); //添加标题
titleParagraph.setAlignment(ParagraphAlignment.CENTER); //设置段落居中
XWPFRun titleParagraphRun = titleParagraph.createRun(); //声明一个段落
titleParagraphRun.setText("XXXX清单"); //设置段落内容,也可以从数据库读数据写入
titleParagraphRun.setColor("000000"); //设置这一行字体颜色
titleParagraphRun.setFontSize(20); //设置这一行字体大小
//两个表格之间加个换行
XWPFParagraph paragraph = document.createParagraph();
XWPFRun paragraphRun = paragraph.createRun();
paragraphRun.setText("\r");
/***方式二***/
XWPFTable ComTable = document.createTable(24,9); //声明一个24行9列的表格
setTableWidthAndHAlign(ComTable, "9024", STJc.CENTER); //设置表的总体宽度
setTableCellMargin(ComTable, 0, 20, 0, 20); //这里相当于css里设置margin的值(上,左,下,右)
//设置列宽
int[] colWidths = new int[] { 4000, 2638, 525, 3692 ,2169,2169,2169,2169,2169};
setTableGridCol(ComTable, colWidths);
//读取数据库相关数据
List
Long ddid=null;
String cce=null;
if(list!=null&&list.size()>0){
NovKjcx tep=list.get(0);
ddid=tep.getId();
cce=tep.getKjcxwt_code();
UserLxr Lxr=tep.getUser_lxr();
ComTable.getRow(0).getCell(0).setText("XXXX委托日期 * "+tep.getKjcxwt_wtrq()); //(0.0)表格的坐标从(0,0)开始
if(tep.getKjcxwt_yqrq()==null){
ComTable.getRow(0).getCell(5).setText("XXXX期望完成日期 * "+tep.getKjcxwt_finishdate()); //(0.5)
}else{
ComTable.getRow(0).getCell(5).setText("XXXX期望完成日期 * "+tep.getKjcxwt_yqrq()); //(0.5)
}
ComTable.getRow(1).getCell(0).setText("委托须知 *"); //(1.0)表格的第一行第0列
ComTable.getRow(1).getCell(1).setText("1.委托人必须按要求认真填写并对所提供资料的真实性及可靠性负责。" +
"查新委托内容经确认并被受理后,则不能随意更改。若委托人要求更改查新内容或增加查新点," +
"则需重新办理查新委托,并按新项目收费,或双方协商酌情增加收费。 " +
"2.项目查新实行先付费制度,委托人与查新机构确认查新内容并按收费标准办理缴款手续后,查新委托方被受理。" +
"3.查新受理日以缴款或汇款凭证传真至本查新机构的日期为准。" +
"4.若有其他情况,请电话咨询。"); //(1.1)
ComTable.getRow(3).getCell(0).setText("项目名称 *"); //(3.0)
ComTable.getRow(3).getCell(1).setText("中文 "+tep.getKjcxwt_cname()); //(3.1)
ComTable.getRow(4).getCell(1).setText("英文 "+tep.getKjcxwt_ename()); //(4.1)
ComTable.getRow(5).getCell(0).setText("委托人"); //(5.0)
ComTable.getRow(5).getCell(1).setText("机构名称 *"); //(5.1)
ComTable.getRow(5).getCell(2).setText(Lxr.getOrg_cname()); //(5.2)
ComTable.getRow(6).getCell(1).setText("邮政编码 *"); //(6.1)
ComTable.getRow(6).getCell(2).setText(Lxr.getOrg_postcode()); //(6.2)
ComTable.getRow(6).getCell(3).setText("通信地址 *"); //(6.3)
ComTable.getRow(6).getCell(4).setText(Lxr.getOrg_address()); //(6.4)
ComTable.getRow(7).getCell(1).setText("项目负责人 *"); //(7.1)
ComTable.getRow(7).getCell(2).setText(Lxr.getFzr_cname()); //(7.2)
ComTable.getRow(7).getCell(3).setText("电话 *"); //(7.3)
ComTable.getRow(7).getCell(4).setText(Lxr.getFzr_tel()); //(7.4)
ComTable.getRow(7).getCell(5).setText("手机 *"); //(7.5)
ComTable.getRow(7).getCell(6).setText(Lxr.getFzr_mobile()); //(7.6)
ComTable.getRow(7).getCell(7).setText("EMAIL *"); //(7.7)
ComTable.getRow(7).getCell(8).setText(Lxr.getFzr_email()); //(7.8)
ComTable.getRow(8).getCell(1).setText("联系人 *"); //(8.1)
ComTable.getRow(8).getCell(2).setText(Lxr.getLxr_cname()); //(8.2)
ComTable.getRow(8).getCell(3).setText("电话 *"); //(8.3)
ComTable.getRow(8).getCell(4).setText(Lxr.getLxr_phone()); //(8.4)
ComTable.getRow(8).getCell(5).setText("手机 *"); //(8.5)
ComTable.getRow(8).getCell(6).setText(Lxr.getLxr_mobile()); //(8.6)
ComTable.getRow(8).getCell(7).setText("EMAIL *"); //(8.7)
ComTable.getRow(8).getCell(8).setText(Lxr.getLxr_email()); //(8.8)
ComTable.getRow(9).getCell(0).setText("XX目的 *"); //(9.0)
ComTable.getRow(9).getCell(1).setText(" XXXX:"); //(9.1)
ComTable.getRow(10).getCell(1).setText("XXXX:"); //(10.1)
ComTable.getRow(11).getCell(1).setText("其他:"); //(11.1)
List
List
for(NovKjcxLX lxx:lxlist){
String mdty=lxx.getCxmd_type();
if("XXXX".equals(mdty)){
for(NovKjcxPurpose pupose:plist){
String pup=pupose.getPurpose();
Long lid=lxx.getId();
if((Long.valueOf(pup)).longValue()==lid.longValue()){
ComTable.getRow(9).getCell(2).setText(lxx.getCxmd_cname()); //(9.2)
}
}
}
if("XXXX".equals(mdty)){
for(NovKjcxPurpose pupose:plist){
String pup=pupose.getPurpose();
Long lid=lxx.getId();
if((Long.valueOf(pup)).longValue()==lid.longValue()){
ComTable.getRow(10).getCell(2).setText(lxx.getCxmd_cname()); //(10.2)
}
}
}
if("其他".equals(mdty)){
for(NovKjcxPurpose pupose:plist){
String pup=pupose.getPurpose();
Long lid=lxx.getId();
if((Long.valueOf(pup)).longValue()==lid.longValue()){
ComTable.getRow(11).getCell(2).setText(lxx.getCxmd_cname()); //(11.2)
}
}
}
}
ComTable.getRow(12).getCell(0).setText("XX范围 *"); //(12.0)
ComTable.getRow(12).getCell(1).setText(tep.getKjcxwt_cxfw()); //(12.1)
ComTable.getRow(13).getCell(0).setText("XXXX的科学技术要点"); //(13.0)
ComTable.getRow(13).getCell(1).setText(tep.getKjcxwt_cxyd()); //(13.1)
ComTable.getRow(14).getCell(0).setText("XX点"); //(14.0)
List
if(cxdlist!=null&&cxdlist.size()>0){
String cxds="";
for(NovKjcxCXD CXD:cxdlist){
cxds +=CXD.getKjyd();
}
ComTable.getRow(14).getCell(1).setText(cxds); //(14.1)
}
ComTable.getRow(15).getCell(0).setText("参考"); //(15.0)
List
if(cxdlist!=null&&cxdlist.size()>0){
String jscs="";
for(NovKjcxKeyword JSC:keywordlist){
jscs +=" 中文:"+JSC.getCkeyword();
jscs +=" 英文:"+JSC.getEkeyword();
}
ComTable.getRow(15).getCell(1).setText(jscs); //(15.1)
}
ComTable.getRow(16).getCell(0).setText("XX分类 *"); //(16.0)
List
if(subList!=null&&subList.size()>0){
for(Subject ssb:subList){
if((ssb.getId()).longValue()==(tep.getKjcx_subject_id().getId()).longValue()){
ComTable.getRow(16).getCell(1).setText(ssb.getSubject_cname()); //(16.1)
}
}
}
ComTable.getRow(17).getCell(0).setText("XX分类 *"); //(17.0)
List
if(industryList!=null&&industryList.size()>0){
for(Subject ius:industryList){
Long ii=ius.getId();
Long tp=tep.getKjcx_chanye_id().getId();
if(ii.longValue()==tp.longValue()){
ComTable.getRow(17).getCell(1).setText(ius.getSubject_cname()); //(17.1)
}
}
}
ComTable.getRow(18).getCell(0).setText("希望完成日期"); //(18.0)
if(tep.getKjcxwt_yqrq()==null){
ComTable.getRow(18).getCell(1).setText(tep.getKjcxwt_finishdate()); //(18.1)
}else{
ComTable.getRow(18).getCell(1).setText(tep.getKjcxwt_yqrq()); //(18.1)
}
ComTable.getRow(19).getCell(0).setText("需要红章份数"); //(19.0)
String rednm =tep.getRednums().toString();
ComTable.getRow(19).getCell(1).setText(rednm); //(19.1)
ComTable.getRow(20).getCell(0).setText("提交方式"); //(20.0)
String sbtype=tep.getSubmit_type().toString();
if("1".equals(sbtype)){
ComTable.getRow(20).getCell(1).setText("EMS快递预付"); //(20.1)
}else if("0".equals(sbtype)){
ComTable.getRow(20).getCell(1).setText("自取"); //(20.1)
}else if("2".equals(sbtype)){
ComTable.getRow(20).getCell(1).setText("顺丰快递到付"); //(20.1)
}
ComTable.getRow(21).getCell(0).setText("指定查新员"); //(21.0)
List
if(userList!=null&&userList.size()>0){
for(User ue:userList){
if((tep.getUser_id_cxyuan().getId()).longValue()==(ue.getId()).longValue()){
ComTable.getRow(21).getCell(1).setText(ue.getCname()); //(21.1)
}
}
}
ComTable.getRow(22).getCell(0).setText("备注"); //(22.0)
ComTable.getRow(22).getCell(1).setText(tep.getKjcxwt_memo()); //(22.1)
ComTable.getRow(23).getCell(0).setText("添加附件"); //(23.0)
List
if(rlist!=null&&rlist.size()>0){
ComTable.getRow(23).getCell(1).setText(rlist.get(0).getLocalurl()); //(23.1)
}
}
ComTable.getRow(0).getCell(0).setColor("E1E6F6"); //(0.0)
ComTable.getRow(0).getCell(5).setColor("E1E6F6"); //(0.5)
//横向合并单元格
mergeCellsHorizontal(ComTable,0,0,4); // 合并第0行的从第0列开始到底4列为一格
mergeCellsHorizontal(ComTable,0,5,8); // 合并第0行的从第5列开始到底8列为一格
mergeCellsHorizontal(ComTable,1,1,8);
mergeCellsHorizontal(ComTable,2,1,8);
mergeCellsHorizontal(ComTable,3,1,8);
mergeCellsHorizontal(ComTable,4,1,8);
mergeCellsHorizontal(ComTable,5,2,8);
mergeCellsHorizontal(ComTable,6,4,8);
mergeCellsHorizontal(ComTable,9,2,8);
mergeCellsHorizontal(ComTable,10,2,8);
mergeCellsHorizontal(ComTable,11,2,8);
mergeCellsHorizontal(ComTable,12,1,8);
mergeCellsHorizontal(ComTable,13,1,8);
mergeCellsHorizontal(ComTable,14,1,8);
mergeCellsHorizontal(ComTable,15,1,8);
mergeCellsHorizontal(ComTable,16,1,8);
mergeCellsHorizontal(ComTable,17,1,8);
mergeCellsHorizontal(ComTable,18,1,8);
mergeCellsHorizontal(ComTable,19,1,8);
mergeCellsHorizontal(ComTable,20,1,8);
mergeCellsHorizontal(ComTable,21,1,8);
mergeCellsHorizontal(ComTable,22,1,8);
mergeCellsHorizontal(ComTable,23,1,8);
//纵向合并单元格
for(int k=0;k<9;k++){//垂直
mergeCellsVertically(ComTable,k,1,2); //合并第k列的第一行和第二行
}
for(int k=0;k<1;k++){//垂直
mergeCellsVertically(ComTable,k,3,4);
}
for(int k=0;k<1;k++){//垂直
mergeCellsVertically(ComTable,k,5,8);
}
for(int k=0;k<1;k++){//垂直
mergeCellsVertically(ComTable,k,9,11);
}
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
/*CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();
//设置为A3纸张
pgsz.setW(BigInteger.valueOf(15840));
pgsz.setH(BigInteger.valueOf(12240));
pgsz.setOrient(STPageOrientation.LANDSCAPE);*/
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
//添加页眉
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "XXXXXXXXX";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
//设置为右对齐
headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
//添加页脚
CTP ctpFooter = CTP.Factory.newInstance();
CTR ctrFooter = ctpFooter.addNewR();
CTText ctFooter = ctrFooter.addNewT();
String footerText = "XXXXXXXX";
ctFooter.setStringValue(footerText);
XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
headerParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFParagraph[] parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerParagraph;
policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
document.write(outs);
outs.close();
System.out.println("create XXXX订单详情单 success.");
data.put("code", "2");
}
if(data!=null)
json=gson.toJson(data); //传递一个参数到json里返回前台,告知是否成功并给予提示信息
return json;
}
// word跨列合并单元格
public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if ( cellIndex == fromCell ) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}
// word跨行并单元格
public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if ( rowIndex == fromRow ) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
/**
* @Description: 设置表格总宽度与水平对齐方式
*/
public static void setTableWidthAndHAlign(XWPFTable table, String width,
STJc.Enum enumValue) {
CTTblPr tblPr = getTableCTTblPr(table);
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr
.addNewTblW();
if (enumValue != null) {
CTJc cTJc = tblPr.addNewJc();
cTJc.setVal(enumValue);
}
tblWidth.setW(new BigInteger(width));
tblWidth.setType(STTblWidth.DXA);
}
public static CTTblPr getTableCTTblPr(XWPFTable table) {
CTTbl ttbl = table.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl
.getTblPr();
return tblPr;
}
/**
* @Description: 设置单元格Margin
*/
public static void setTableCellMargin(XWPFTable table, int top, int left,
int bottom, int right) {
table.setCellMargins(top, left, bottom, right);
}
/**
* @Description: 设置表格列宽
*/
public static void setTableGridCol(XWPFTable table, int[] colWidths) {
CTTbl ttbl = table.getCTTbl();
CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid()
: ttbl.addNewTblGrid();
for (int j = 0, len = colWidths.length; j < len; j++) {
CTTblGridCol gridCol = tblGrid.addNewGridCol();
gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
}
}
/**
* @Description: 设置行高
*/
public static void setRowHeight(XWPFTableRow row, String hight,
STHeightRule.Enum heigthEnum) {
CTTrPr trPr = getRowCTTrPr(row);
CTHeight trHeight;
if (trPr.getTrHeightList() != null && trPr.getTrHeightList().size() > 0) {
trHeight = trPr.getTrHeightList().get(0);
} else {
trHeight = trPr.addNewTrHeight();
}
trHeight.setVal(new BigInteger(hight));
if (heigthEnum != null) {
trHeight.setHRule(heigthEnum);
}
}
public static CTTrPr getRowCTTrPr(XWPFTableRow row) {
CTRow ctRow = row.getCtRow();
CTTrPr trPr = ctRow.isSetTrPr() ? ctRow.getTrPr() : ctRow.addNewTrPr();
return trPr;
}
}