spire.doc操作word文档

/**
     * 	导入申报方案
     * 	1.准备基础数据,
     * 	2.将基本书签数据插入到word文档
     * 	3.导入实施方案表格数据
     * 	4.导入项目预算表格数据
     * 	5.导入执行机构表格数据
     * @param bean
     * @return
     */
	public EntityBean exportDeclareScheme(EntityBean bean) {
		System.out.println(bean);
		//step 1  准备基础数据,
		String zmtzid = bean.getString("id");
		String community = bean.getString("community");//申报社区
		String supplier_name = bean.getString("supplier_name");//供应商名称
		String supplier_address = bean.getString("supplier_address");//供应商地址
		String jigouclrq = bean.getString("jigouclrq").substring(0,10);//机构成立日期
		String email = bean.getString("email");//机构邮箱地址
		String legal_person = bean.getString("legal_person");//负责人
		String legal_phone = bean.getString("legal_phone");//负责人联系方式
		String contact_person = bean.getString("contact_person");//对接人
		String contact_phone = bean.getString("contact_phone");//对接人联系方式
		String jigoulb = bean.getString("jigoulb");//机构类别		单选
		String project_type = bean.getString("project_type");//项目类别  	单选
		String fuwulb = bean.getString("fuwulb");//服务类别	单选
		String project_name = bean.getString("project_name");//项目名称
		String project_code = bean.getString("project_code");//项目编号
		String xmyszje = bean.getString("xmyszje");//项目预算总额
		String project_content = bean.getString("project_content");//项目内容
		String project_obj_sub = bean.getString("project_obj_sub");//项目总目标及分目标
		String service_groups = bean.getString("service_groups");//主要服务人群
		String implement_site = bean.getString("implement_site");//实施地点
		String xiangmuyqcx = bean.getString("xiangmuyqcx");//项目预期成效
		String xiangmucgjy = bean.getString("xiangmucgjy");//项目成功经验
		String yingyezzjzzFileStr = bean.getString("yingyezzjzz");//营业执照及资质			附件
		String othersFileStr = bean.getString("others");//其它			附件
		String team_qualificationFileStr = bean.getString("team_qualification");//人员资质			附件
		String pssuggest = bean.getString("pssuggest");
		String project_starttime = bean.getString("project_starttime");
		String project_endtime = bean.getString("project_endtime");
		String project_service_cycle = project_starttime.substring(0,10) + " - " + project_endtime.substring(0,10);
		//step 1 end
		
		NameedLogic l = NameedPathUtil.getMapLogic("default");
		String defpath = l.getPath();
		String string = "template/template_declareScheme_fw.doc";
		File wordFilePath = File.get(defpath,string);
		Document doc = new Document();
		//读取模板文件
		doc.loadFromFile(wordFilePath.getAbsolutePath());
		BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
		//移动到指定书签位置
		
		//step 2 将基本书签数据插入到word文档
		bookmarksNavigator.moveToBookmark("community");
		bookmarksNavigator.insertText(community);
		bookmarksNavigator.moveToBookmark("supplier_name");
		bookmarksNavigator.insertText(supplier_name);
		bookmarksNavigator.moveToBookmark("supplier_address");
		bookmarksNavigator.insertText(supplier_address);
		bookmarksNavigator.moveToBookmark("jigouclrq");
		bookmarksNavigator.insertText(jigouclrq);
		bookmarksNavigator.moveToBookmark("email");
		bookmarksNavigator.insertText(email);
		bookmarksNavigator.moveToBookmark("legal_person");
		bookmarksNavigator.insertText(legal_person);
		bookmarksNavigator.moveToBookmark("legal_phone");
		bookmarksNavigator.insertText(legal_phone);
		bookmarksNavigator.moveToBookmark("contact_person");
		bookmarksNavigator.insertText(contact_person);
		bookmarksNavigator.moveToBookmark("contact_phone");
		bookmarksNavigator.insertText(contact_phone);
		bookmarksNavigator.moveToBookmark("jigoulb");
		bookmarksNavigator.insertText(CodeTypeCache.getInstance().getCodeValue("mswss_xmk_jglb", jigoulb).getcodevalue());
		bookmarksNavigator.moveToBookmark("project_type");
		bookmarksNavigator.insertText(CodeTypeCache.getInstance().getCodeValue("mswss_xmk_xmlb", project_type).getcodevalue());
		bookmarksNavigator.moveToBookmark("fuwulb");
		bookmarksNavigator.insertText(CodeTypeCache.getInstance().getCodeValue("mswss_xmk_fwlb", fuwulb).getcodevalue());
		bookmarksNavigator.moveToBookmark("project_name");
		bookmarksNavigator.insertText(project_name);
		bookmarksNavigator.moveToBookmark("project_code");
		bookmarksNavigator.insertText(project_code);
		bookmarksNavigator.moveToBookmark("xmyszje");
		bookmarksNavigator.insertText(xmyszje);
		bookmarksNavigator.moveToBookmark("project_content");
		bookmarksNavigator.insertText(project_content);
		bookmarksNavigator.moveToBookmark("project_obj_sub");
		bookmarksNavigator.insertText(project_obj_sub);
		bookmarksNavigator.moveToBookmark("service_groups");
		bookmarksNavigator.insertText(service_groups);
		bookmarksNavigator.moveToBookmark("implement_site");
		bookmarksNavigator.insertText(implement_site);
		bookmarksNavigator.moveToBookmark("xiangmuyqcx");
		bookmarksNavigator.insertText(xiangmuyqcx);
		bookmarksNavigator.moveToBookmark("xiangmucgjy");
		bookmarksNavigator.insertText(xiangmucgjy);
		bookmarksNavigator.moveToBookmark("pssuggest");
		bookmarksNavigator.insertText(pssuggest);
		bookmarksNavigator.moveToBookmark("project_service_cycle");
		bookmarksNavigator.insertText(project_service_cycle);
		// step 2 end
		 
		// step 3	导入实施方案表格数据
		/**
		 * 	准备表格的表头和数据内容
		 */
//		dSource.dynaExecuteSQL("select * from mswss_zmtz where id = '" + zmtzid+ "'")
		try {
			String getSSFASqlString = "select * from mswss_qjfwxmssfa where qujixmid = '" + zmtzid + "'";
			EntityBean[] ssfaBeans = dSource.dynaExecuteSQL(getSSFASqlString);
			if(ssfaBeans != null && ssfaBeans.length > 0) {
				String[] header = {"活动名称", "实施周期", "活动形式", "活动内容","受益人数"};
				String[][] data = new String[ssfaBeans.length][header.length];  //
				for (int i = 0; i < ssfaBeans.length; i++) {
					EntityBean entityBean = ssfaBeans[i];
					String[] s = new String[header.length];
					s[0] = entityBean.getString("huodongmc");
					s[1] = entityBean.getString("shishizq");
					s[2] = entityBean.getString("huodongxs");
					s[3] = entityBean.getString("huodongnr");
					s[4] = entityBean.getString("shouyirs");
					data[i] = s;
				}
				//添加表格
		        Table table = getTable(doc, header, data);
		        bookmarksNavigator.moveToBookmark("project_ssfa");
				bookmarksNavigator.insertTable(table);
				table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);//列宽自动适应内容
		        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);//表格自适应列宽
		        doc.getBookmarks().remove(bookmarksNavigator.getCurrentBookmark());
			}
		} catch (Exception e) {
			e.printStackTrace();
			Global.getInstance().LogError("插入实施方案表格数据出错");
		}
		// setp 3 end
		
		// step 4 导入项目预算表格数据
		try {
			String getSql = "select * from mswss_xmkxmys where xmid = '" + zmtzid + "'";
			EntityBean[] beans = dSource.dynaExecuteSQL(getSql);
			if(beans != null && beans.length > 0) {
				String[] header = {"序号", "费用类别", "具体科目", "费用标准(单价)","数量","小计","备注"};
				String[][] data = new String[beans.length][header.length];  //
				for (int i = 0; i < beans.length; i++) {
					EntityBean entityBean = beans[i];
					String[] s = new String[header.length];
					s[0] = String.valueOf(i + 1);
					s[1] = entityBean.getString("shishizq");
					s[2] = entityBean.getString("jutikm");
					s[3] = entityBean.getString("danjia");
					s[4] = entityBean.getString("shuliang");
					s[5] = entityBean.getString("feiyongxj");
					s[6] = entityBean.getString("beizhu");
					data[i] = s;
				}
				//添加表格
		        Table table = getTable(doc, header, data);
		        bookmarksNavigator.moveToBookmark("project_xmys");
				bookmarksNavigator.insertTable(table);
				table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);//列宽自动适应内容
		        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);//表格自适应列宽
		        doc.getBookmarks().remove(bookmarksNavigator.getCurrentBookmark());
			}
		} catch (Exception e) {
			e.printStackTrace();
			Global.getInstance().LogError("插入项目预算表格数据出错");
		}
		// step 4 end
		
		// step 5 导入执行机构表格数据
		try {
			String getSql = "select * from mswss_qjxmzxjg where qujixmid = '" + zmtzid + "'";
			EntityBean[] beans = dSource.dynaExecuteSQL(getSql);
			if(beans != null && beans.length > 0) {
				String[] header = {"序号", "姓名", "学历", "职务","项目经验"};
				String[][] data = new String[beans.length][header.length];  //
				for (int i = 0; i < beans.length; i++) {
					EntityBean entityBean = beans[i];
					String[] s = new String[header.length];
					s[0] = String.valueOf(i + 1);
					s[1] = entityBean.getString("xingming");
					s[2] = entityBean.getString("xueli");
					s[3] = entityBean.getString("zhiwu");
					s[4] = entityBean.getString("xiangmujy");
					data[i] = s;
				}
				//添加表格
				Table table = getTable(doc, header, data);
				bookmarksNavigator.moveToBookmark("project_zxjg");
				bookmarksNavigator.insertTable(table);
				table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);//列宽自动适应内容
				table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);//表格自适应文档纸张列宽
				doc.getBookmarks().remove(bookmarksNavigator.getCurrentBookmark());
			}
		} catch (Exception e) {
			e.printStackTrace();
			Global.getInstance().LogError("插入执行团队表格数据出错");
		}
		// step 5 end
		
		String yyzzStr = getFileDownLoadUrl(yingyezzjzzFileStr);
		String otherStr = getFileDownLoadUrl(othersFileStr);
		String ryzzStr = getFileDownLoadUrl(team_qualificationFileStr);
		// setp 6 插入附件信息,以超链接的形式插入
		if(yyzzStr != null) {
			//营业执照及资质附件
			Paragraph p = new Paragraph(doc);
			p.appendHyperlink(yyzzStr,"营业执照及资质附件",HyperlinkType.File_Link);
			bookmarksNavigator.moveToBookmark("yingyezzjzz");
			bookmarksNavigator.insertParagraph(p);
		}
		if(otherStr != null) {
			//营业执照及资质附件
			Paragraph p = new Paragraph(doc);
			p.appendHyperlink(otherStr,"其它附件",HyperlinkType.File_Link);
			bookmarksNavigator.moveToBookmark("others");
			bookmarksNavigator.insertParagraph(p);
		}
		if(ryzzStr != null) {
			//营业执照及资质附件
			Paragraph p = new Paragraph(doc);
			p.appendHyperlink(ryzzStr,"人员资质附件",HyperlinkType.File_Link);
			bookmarksNavigator.moveToBookmark("team_qualification");
			bookmarksNavigator.insertParagraph(p);
		}
		// setp 6 end
//		
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String dataStr = sdf.format(new Date());
        File file = File.get(defpath, dataStr);
        if(!file.exists()){
        	file.mkdirs();
        }
        String fileName = project_name + "申报方案";//文件名
        String finalFileName = file  + "\\" + fileName + ".docx";
        doc.saveToFile(finalFileName,FileFormat.Docx_2013);
		doc.close();
		EntityBean res = new EntityBean();
		res.set("url",SERVER_URL + "/LEAP/Download/default/" + dataStr + "/" + fileName + ".docx");
		res.set("path",finalFileName);
		return res;
	}

	private Table getTable(Document doc, String[] header, String[][] data) {
		Table table = new Table(doc);
        //设置表格的行数和列数
        table.resetCells(data.length + 1, header.length);
        //设置第一行作为表格的表头并添加数据
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        row.setHeight(40);
        row.setHeightType(TableRowHeightType.Exactly);
        row.getRowFormat().setBackColor(Color.white);
        for (int i = 0; i < header.length; i++) {
            row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
            Paragraph p = row.getCells().get(i).addParagraph();
            p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            TextRange range1 = p.appendText(header[i]);
            range1.getCharacterFormat().setFontName("Arial");
            range1.getCharacterFormat().setFontSize(12f);
            range1.getCharacterFormat().setBold(true);
        }

        //添加数据到剩余行
        for (int r = 0; r < data.length; r++) {
            TableRow dataRow = table.getRows().get(r + 1);
            dataRow.setHeight(25);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            dataRow.getRowFormat().setBackColor(Color.white);
            for (int c = 0; c < data[r].length; c++) {
                dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
                range2.getCharacterFormat().setFontName("Arial");
                range2.getCharacterFormat().setFontSize(10f);
            }
        }
		return table;
	}


你可能感兴趣的:(java,java)