Aspose.Word之通過模板(書籤)匯出word文件,並刪除多餘的單元格
阿新 • • 發佈:2019-02-07
public partial class WordExport_KJYJJRHLTHTS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string dname = Request["dname"];
string DWBH = Request["DWBH"];
string XHBH = Request["XHBH"];
string templatePath = HttpContext.Current.Server.MapPath(@"~/office/doc/模板.doc");
System.Data.DataTable dt = DbSqlHelper.GetDataSet("SELECT * FROM KJYJJRHLTHTS WHERE XHBH='" + XHBH + "' and DWBH='" + DWBH + "' and MBBH='" + dname + "'").Tables[0];
List<Dictionary<string, object>> Datalist = DataTableToListDic(dt);
Document doc = new Document(templatePath);
DocumentBuilder builder = new DocumentBuilder(doc);
// 記錄空行
Dictionary<string, int> emptyRow = new Dictionary<string, int>()
{
{"CJRY_NAME",0},{"JFZC_ZCNR",0},{"XMJD_JFYS",0},{"ZMQYJ_NAME",0},{"ZMZJ_NAME",0},{"DWQK_CJDW",0}
};
// 記錄每個動態表格的最後一行在總表的位置(假設所有動態資料為20行)
Dictionary<string, int> endRow = new Dictionary<string, int>()
{
{"CJRY_NAME",148},{"JFZC_ZCNR",124},{"XMJD_JFYS",102},{"ZMQYJ_NAME",75},{"ZMZJ_NAME",53},{"DWQK_CJDW",21}
};
// 動態資料最大行數
const int MaxRow = 20;
foreach (var item in Datalist[0])
{
foreach (Bookmark mark in doc.Range.Bookmarks)
{
if (mark != null)
{
if (mark.Name == item.Key.ToString())
{
mark.Text = Datalist[0][item.Key.ToString()].ToString();
if(item.Value == null || String.IsNullOrEmpty(item.Value.ToString())){
if (item.Key.Contains("CJRY_NAME"))
{
emptyRow["CJRY_NAME"]++;
}
if (item.Key.Contains("JFZC_ZCNR"))
{
emptyRow["JFZC_ZCNR"]++;
}
if (item.Key.Contains("XMJD_JFYS") )
{
emptyRow["XMJD_JFYS"]++;
}
if (item.Key.Contains("ZMQYJ_NAME"))
{
emptyRow["ZMQYJ_NAME"]++;
}
if (item.Key.Contains("ZMZJ_NAME"))
{
emptyRow["ZMZJ_NAME"]++;
}
if (item.Key.Contains("DWQK_CJDW"))
{
emptyRow["DWQK_CJDW"]++;
}
}
}
}
}
}
// 根據儲存的空行數量刪除相應的行
for (int i = 0; i < emptyRow["CJRY_NAME"]; i++)
builder.DeleteRow(0, endRow["CJRY_NAME"]--);
for (int i = 0; i < emptyRow["JFZC_ZCNR"]; i++)
builder.DeleteRow(0, endRow["JFZC_ZCNR"]--);
for (int i = 0; i < emptyRow["XMJD_JFYS"]; i++)
builder.DeleteRow(0, endRow["XMJD_JFYS"]--);
for (int i = 0; i < emptyRow["ZMQYJ_NAME"]; i++)
builder.DeleteRow(0, endRow["ZMQYJ_NAME"]--);
for (int i = 0; i < emptyRow["ZMZJ_NAME"]; i++)
builder.DeleteRow(0, endRow["ZMZJ_NAME"]--);
for (int i = 0; i < emptyRow["DWQK_CJDW"]; i++)
builder.DeleteRow(0, endRow["DWQK_CJDW"]--);
doc.Save("1.doc", SaveFormat.Doc, SaveType.OpenInWord, this.Response);
}
}
/// <summary> 將 DataTable 轉換為 List[Dictionary[string, object]] </summary>
/// <param name="dt">DataTable</param>
/// <returns>List[Dictionary[string, object]]</returns>
public static List<Dictionary<string, object>> DataTableToListDic(System.Data.DataTable dt)
{
List<Dictionary<string, object>> listDic = new List<Dictionary<string, object>>();
if (dt == null || dt.Rows.Count < 0) return null;
for (int i = 0; i < dt.Rows.Count; i++)
{
listDic.Add(new Dictionary<string, object>());
for (int j = 0; j < dt.Columns.Count; j++)
{
listDic[i].Add(dt.Columns[j].ToString(), dt.Rows[i][j]);
}
}
return listDic;
}
}