首页 > 代码库 > 操作word文档 添加一个table (书签方式)
操作word文档 添加一个table (书签方式)
1.添加一个table 显示成问答两列
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Words;
using TJSupervisor.ModulePluginsHandler.Business.Service;
namespace HY.DevelopmentWeb.ProjectWeb.Page.InquiryRecordPrint
{
public partial class InquiryRecordWord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
int CaseId;
string EmpId;
if (Request.QueryString["Id"] != null)
{
CaseId = Convert.ToInt32(Request.QueryString["Id"].ToString());
EmpId = Request.QueryString["EmpId"].ToString();
}
else
{
return;
}
InquiryRecordPrintService cs = new InquiryRecordPrintService();
DataSet ds = new DataSet();
ds = cs.GetInquiryRecordPrint(CaseId, EmpId);
string tmppath = Server.MapPath("~/Data/InquiryRecordPrint.doc");
string dataDir = Server.MapPath("~/Data/");
Document doc = new Document(tmppath); //载入模板
DocumentBuilder builder = new DocumentBuilder(doc);
doc.Range.Replace("{{InquiriedEmpName}}", ds.Tables[0].Rows[0]["InquiriedEmpName"].ToString(), false, false);
doc.Range.Replace("{{Gender}}", ds.Tables[0].Rows[0]["Gender"].ToString(), false, false);
doc.Range.Replace("{{Marriage}}", ds.Tables[0].Rows[0]["Marriage"].ToString(), false, false);
doc.Range.Replace("{{OrgName}}", ds.Tables[0].Rows[0]["OrgName"].ToString(), false, false);
doc.Range.Replace("{{EmpJob}}", ds.Tables[0].Rows[0]["EmpJob"].ToString(), false, false);
doc.Range.Replace("{{EntryTime}}", ds.Tables[0].Rows[0]["EmployedDate"].ToString(), false, false);
doc.Range.Replace("{{PermanentAddr}}", ds.Tables[0].Rows[0]["PermanentAddr"].ToString(), false, false);
doc.Range.Replace("{{ResidenceAddr}}", ds.Tables[0].Rows[0]["ResidenceAddr"].ToString(), false, false);
doc.Range.Replace("{{ContactTel}}", ds.Tables[0].Rows[0]["ContactTel"].ToString(), false, false);
doc.Range.Replace("{{IdentityNo}}", ds.Tables[0].Rows[0]["IdentityNo"].ToString(), false, false);
doc.Range.Replace("{{InquiryDateBegin}}", ds.Tables[0].Rows[0]["InquiryDateBegin"].ToString(), false, false);
doc.Range.Replace("{{InquiryDateEnd}}", ds.Tables[0].Rows[0]["InquiryDateEnd"].ToString(), false, false);
doc.Range.Replace("{{InquiryPlace}}", ds.Tables[0].Rows[0]["InquiryPlace"].ToString(), false, false);
doc.Range.Replace("{{InquiryItems}}", ds.Tables[0].Rows[0]["InquiryItems"].ToString(), false, false);
//Insert some table
bool isbool = builder.MoveToBookmark("Table");
if (isbool)
{
builder.StartTable();
for (int i = 0; i < (ds.Tables[1].Rows.Count * 2); i++)
{
builder.InsertCell(); //动态添加列
builder.Font.Name = "微软雅黑";
builder.Font.Size = 10;
builder.Bold = false;
builder.CellFormat.Width = 800;
builder.CellFormat.Borders.Right.Color = System.Drawing.Color.White;//改变左右边框为白色
builder.CellFormat.Borders.Left.Color = System.Drawing.Color.White;
if (i % 2 == 0)
{
builder.Write("问(" + ((i / 2) + 1).ToString() + ")" + ds.Tables[1].Rows[(i / 2)]["Question"].ToString());
}
else
{
builder.Write("答(" + (((i - 1) / 2) + 1).ToString() + ")" + ds.Tables[1].Rows[((i - 1) / 2)]["Answer"].ToString());
}
builder.EndRow();
}
builder.EndTable();
}
string fullName = "InquiryRecordPrint_" + System.DateTime.Now.Millisecond.ToString();
//save word
doc.Save(Response, "询问笔录_" + ds.Tables[0].Rows[0]["InquiryItems"].ToString(), ContentDisposition.Attachment, new Aspose.Words.Saving.DocSaveOptions(SaveFormat.Doc));
Context.Response.Write("{success:‘true‘,fullName:‘" + fullName + "‘}");
}
catch (Exception ex)
{
Context.Response.Write("{success:‘false‘,fullName:‘" + ex.Message + "‘}");
}
}
}
}
2.靠查出来的数据循环table的行
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Words;
using TJSupervisor.ModulePluginsHandler.Business.Service;
namespace HY.DevelopmentWeb.ProjectWeb.Page.CaseReportPrint
{
public partial class CaseReportWord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
int CaseId;
if (Request.QueryString["Id"] != null)
{
CaseId = Convert.ToInt32(Request.QueryString["Id"].ToString());
}
else
{
return;
}
CaseReportPrintService cs = new CaseReportPrintService();
DataSet ds = new DataSet();
ds = cs.GetPrintCaseInfo(CaseId);
string tmppath = Server.MapPath("~/Data/CaseReport.docx");
string dataDir = Server.MapPath("~/Data/");
Document doc = new Document(tmppath); //载入模板
DocumentBuilder builder = new DocumentBuilder(doc);
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// DataRow dr = ds.Tables[0].Rows[i];
// doc.Range.Replace("{{replace}}", dr["EmpName"].ToString() + dr["gender"].ToString() + dr["Entrytime"].ToString() + "入职途家,现任" + dr["job"].ToString(), false, false); // {{EmpName}},{{gender}},{{Entrytime}}入职途家,现任{{job}}。
// //doc.Range.Replace("{{EmpName}}", dr["EmpName"].ToString(), false, false);
// //doc.Range.Replace("{{gender}}", dr["gender"].ToString(), false, false);
// //doc.Range.Replace("{{Entrytime}}", dr["Entrytime"].ToString(), false, false);
// //doc.Range.Replace("{{job}}", dr["job"].ToString(), false, false);
//}
bool isbool = builder.MoveToBookmark("Table");
if (isbool)
{
builder.StartTable();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
builder.InsertCell(); //动态添加列
builder.Font.Name = "宋体";
builder.Font.Size = 12;
builder.Bold = false;
builder.CellFormat.Width = 800;
builder.CellFormat.Borders.Right.Color = System.Drawing.Color.White;//改变左右边框为白色
builder.CellFormat.Borders.Left.Color = System.Drawing.Color.White;
builder.CellFormat.Borders.Top.Color = System.Drawing.Color.White;//改变左右边框为白色
builder.CellFormat.Borders.Bottom.Color = System.Drawing.Color.White;
//doc.Range.Replace("{{replace}}", ds.Tables[0].Rows[i]["EmpName"].ToString() + ds.Tables[0].Rows[i]["gender"].ToString() + ds.Tables[0].Rows[i]["Entrytime"].ToString() + "入职途家,现任" + ds.Tables[0].Rows[i]["job"].ToString(), false, false); // {{EmpName}},{{gender}},{{Entrytime}}入职途家,现任{{job}}。
builder.Write(ds.Tables[0].Rows[i]["EmpName"].ToString() +"," +ds.Tables[0].Rows[i]["gender"].ToString()+"," + ds.Tables[0].Rows[i]["Entrytime"].ToString() + "入职途家,现任" + ds.Tables[0].Rows[i]["job"].ToString()); // {{EmpName}},{{gender}},{{Entrytime}}入职途家,现任{{job}}。
builder.EndRow();
}
builder.EndTable();
}
doc.Range.Replace("{{RegisterSN}}", ds.Tables[0].Rows[0]["RegisterSN"].ToString(), false, false);
doc.Range.Replace("{{Name}}", ds.Tables[0].Rows[0]["Name"].ToString(), false, false);
doc.Range.Replace("{{OccurDateBegin}}", ds.Tables[0].Rows[0]["OccurDateBegin"].ToString(), false, false);
doc.Range.Replace("{{OccurDateEnd}}", ds.Tables[0].Rows[0]["OccurDateEnd"].ToString(), false, false);
doc.Range.Replace("{{OccurPlace}}", ds.Tables[0].Rows[0]["OccurPlace"].ToString(), false, false);
doc.Range.Replace("{{OccurOrgName}}", ds.Tables[0].Rows[0]["OccurOrgName"].ToString(), false, false);
doc.Range.Replace("{{InspectorEmpName}}", ds.Tables[0].Rows[0]["InspectorEmpName"].ToString(), false, false);
doc.Range.Replace("{{Data04}}", ds.Tables[0].Rows[0]["job"].ToString(), false, false);
doc.Range.Replace("{{JointEmpNames}}", ds.Tables[0].Rows[0]["JointEmpNames"].ToString(), false, false);
doc.Range.Replace("{{RegisterDate}}", ds.Tables[0].Rows[0]["RegisterDate"].ToString(), false, false);
doc.Range.Replace("{{AcceptDate}}", ds.Tables[0].Rows[0]["AcceptDate"].ToString(), false, false);
doc.Range.Replace("{{Channel}}", ds.Tables[0].Rows[0]["Channel"].ToString(), false, false);
doc.Range.Replace("{{Step}}", ds.Tables[0].Rows[0]["Step"].ToString(), false, false);
doc.Range.Replace("{{Discovery}}", ds.Tables[0].Rows[0]["Discovery"].ToString(), false, false);
doc.Range.Replace("{{Conclusion}}", ds.Tables[0].Rows[0]["Conclusion"].ToString(), false, false);
doc.Range.Replace("{{Suggestion}}", ds.Tables[0].Rows[0]["Suggestion"].ToString(), false, false);
doc.Range.Replace("{{DateTime}}", ds.Tables[0].Rows[0]["DateTime"].ToString(), false, false);
string fullName = "CaseReportWord_" + System.DateTime.Now.Millisecond.ToString();
//save word
doc.Save(Response, "调查报告_" + ds.Tables[0].Rows[0]["Name"].ToString(),ContentDisposition.Attachment, new Aspose.Words.Saving.DocSaveOptions(SaveFormat.Doc));
Context.Response.Write("{success:‘true‘,fullName:‘" + fullName + "‘}");
}
catch (Exception ex)
{
Context.Response.Write("{success:‘false‘,fullName:‘" + ex.Message + "‘}");
}
}
}
}
操作word文档 添加一个table (书签方式)