• 动态表格写入 word(zml)


    动态表格 写入 word
    //1-5

    protected void exportWord_Click(object sender, EventArgs e)
    {
    hfSelectedIDS.Text = Common.SyncSelectedRowIndexArrayToHiddenField(hfSelectedIDS.Text, Grid1);
    List<int> ids = Common.GetSelectedRowIndexArrayFromHiddenField(hfSelectedIDS.Text);
    if (ids.Count == 0)
    {
    Alert.Show("至少选择一个委托协议!");
    return;
    }
    FileName.Text = DateTime.Now.ToString("yyyyMMddhhmmss") + ".docx";
    string SavaFilesPath = Server.MapPath(("~\UploadFiles\") + FileName.Text);
    string DotFileName = Server.MapPath("~\dot\") + "baojiadan.docx";
    Novacode.DocX wordDocTmp = null;
    Novacode.DocX wordDoc = null;

    //1/5 定义 StringBuilder 放入table的静态部分
    var sbTable = new StringBuilder(@"<table style=""500px;word-break:break-all; word-wrap:break-all;"">
    <tr style="" border-bottom: 1px solid black;font-family: 黑体; text-align: center; font-size: small; font-weight: bold;"">
    <td>
    项目<br/>Item</td>
    <td>
    数量<br/>Qty</td>
    <td>
    实验说明<br/>TestDescription</td>
    <td>
    立项费<br/>Set-Up Charge</td>
    <td>
    单价<br/>Price</td>
    <td>
    计价单位<br/>Charge By</td>
    <td>
    合计<br/>Item Total</td>
    </tr>
    ");

    string pic20="";

    int i = 0;
    foreach (int AID in ids)
    {
    wordDoc = WordReportDocX.LoadTemplateDocument(DotFileName);

    //QRCodeDescriptor qr = QRCodeDescriptor.Init(n.SampleName);
    //string tmp = Server.MapPath(string.Format("~\UploadFiles\{0}.png", DateTime.Now.ToString("yyyyMMddhhmmss")));
    //qr.Render(tmp);
    //Novacode.Image img = wordDoc.AddImage(tmp);
    //WordReportDocX.InsertPicture(wordDoc, "", tmp, 50, 50);
    //var picture = img.CreatePicture(QRCodeDescriptor.height, QRCodeDescriptor.width);
    //wordDoc.Paragraphs[0].AppendPicture(picture);
    //System.IO.File.Delete(tmp);

    List<dYWZCSampleByMainAID> list1 = Govaze.SQLServerDAL.Factory.getCHINADADAL().d_YWZC_dis_Report_SYD_bySampleAID(AID, getUserName());
    foreach (dYWZCSampleByMainAID n1 in list1)
    {
    WordReportDocX.InsertText(wordDoc, "$SampleStatus$", n1.SampleStatus);

    WordReportDocX.InsertText(wordDoc, "$SampleCode$", n1.SampleCode);

    WordReportDocX.InsertText(wordDoc, "$Text2$", n1.Text2);
    WordReportDocX.InsertText(wordDoc, "$Text3$", n1.Text3);

    WordReportDocX.InsertText(wordDoc, "$Text4$", n1.Text4);
    WordReportDocX.InsertText(wordDoc, "$Text5$", n1.Text5);
    WordReportDocX.InsertText(wordDoc, "$Text6$", n1.Text6);

    WordReportDocX.InsertText(wordDoc, "$Text7$", n1.Text7);
    WordReportDocX.InsertText(wordDoc, "$Text8$", n1.Text8);
    WordReportDocX.InsertText(wordDoc, "$Text9$", n1.Text9);
    WordReportDocX.InsertText(wordDoc, "$Text10$", n1.Text10);

    WordReportDocX.InsertText(wordDoc, "$Text11$", n1.Text11);
    WordReportDocX.InsertText(wordDoc, "$Text13$", n1.Text13);

    WordReportDocX.InsertText(wordDoc, "$Text21$", n1.Text21);
    WordReportDocX.InsertText(wordDoc, "$Text22$", n1.Text22);
    WordReportDocX.InsertText(wordDoc, "$Text23$", n1.Text23);
    WordReportDocX.InsertText(wordDoc, "$Text24$", n1.Text24);
    WordReportDocX.InsertText(wordDoc, "$Text25$", n1.Text25);


    WordReportDocX.InsertText(wordDoc, "$Text26$", n1.Text26);
    WordReportDocX.InsertText(wordDoc, "$Text27$", n1.Text27);
    WordReportDocX.InsertText(wordDoc, "$Text28$", n1.Text28);
    WordReportDocX.InsertText(wordDoc, "$Text29$", n1.Text29);

    WordReportDocX.InsertText(wordDoc, "$Text30$", n1.Text30);
    WordReportDocX.InsertText(wordDoc, "$Text31$", n1.Text31);

    WordReportDocX.InsertText(wordDoc, "$Text32$", n1.Text32);
    WordReportDocX.InsertText(wordDoc, "$Text33$", n1.Text33);

    WordReportDocX.InsertText(wordDoc, "$Text34$", n1.Text34);
    //WordReportDocX.InsertText(wordDoc, "$Text35$", n1.Text35);

    WordReportDocX.InsertText(wordDoc, "$Text101$", n1.Text101);
    WordReportDocX.InsertText(wordDoc, "$SnID$", n1.Text102);

    WordReportDocX.InsertText(wordDoc, "$Text103$", n1.Text103);

    WordReportDocX.InsertText(wordDoc, "$Text104$", n1.Text104);
    WordReportDocX.InsertText(wordDoc, "$Text105$", n1.Text105);
    WordReportDocX.InsertText(wordDoc, "$Text108$", n1.Text108);


    WordReportDocX.InsertText(wordDoc, "$ClientName1$", n1.ClientName1);
    WordReportDocX.InsertText(wordDoc, "$ClientName2$", n1.ClientName2);
    WordReportDocX.InsertText(wordDoc, "$ClientName3$", n1.ClientName3);

    WordReportDocX.InsertText(wordDoc, "$Num103$", n1.Num103.ToString());


    pic20 = n1.Text35;

    break;
    }

    List<d_YWZC_dis_Sample_TestItem_CE_Model> list2 = Govaze.SQLServerDAL.Factory.geta17025DAL().d_YWZC_dis_Report_SYD_bySampleAID_TestItem(AID, getUserName());
    if (list2.Count > 0)
    {
    WordReportDocX.AddRow(wordDoc, 1, list2.Count);
    }
    else
    {
    WordReportDocX.RemoveRow(wordDoc, 1, 1);
    }
    int rowNumber = 2;
    foreach (d_YWZC_dis_Sample_TestItem_CE_Model s in list2)
    {

    //2/5 在循环中动态加载行
    sbTable.AppendFormat(@"
    <tr style=""font-size: small; text-align: center;"">
    <td>{0}</td>
    <td>{1}</td>
    <td>{2}</td>
    <td>{3}</td>
    <td>{4}</td>
    <td>{5}</td>
    <td>{6}</td>
    </tr>",
    (rowNumber - 1).ToString(),
    s.Text001,
    s.Text002,
    s.ItemType,
    s.ItemName,
    s.Text004,
    s.Text003);


    //3/5 加载合并行
    bool retVal;
    string Result;
    retVal = Govaze.SQLServerDAL.Factory.geta17025DAL().FindVlaue("BSReamrk",null,null,s.AID,0,getUserName(),out Result);
    if (String.IsNullOrEmpty(Result))
    { }
    else {
    sbTable.AppendFormat(@"
    <tr style=""font-size: x-small; text-align: left;"">
    <td colspan=""7"">{0}</td>
    </tr>", Result);
    }

    //WordReportDocX.InsertCell(wordDoc, 1, rowNumber, 1, (rowNumber - 1).ToString());
    //WordReportDocX.InsertCell(wordDoc, 1, rowNumber, 2, s.Text001);
    //WordReportDocX.InsertCell(wordDoc, 1, rowNumber, 3, s.Text002);
    //WordReportDocX.InsertCell(wordDoc, 1, rowNumber, 4, s.ItemType);
    //WordReportDocX.InsertCell(wordDoc, 1, rowNumber, 5, s.ItemName);
    //WordReportDocX.InsertCell(wordDoc, 1, rowNumber, 6, s.Text004);
    //WordReportDocX.InsertCell(wordDoc, 1, rowNumber, 7, s.Text003);

    rowNumber++;
    }
    if (i == 0)
    {
    wordDoc.SaveAs(SavaFilesPath);
    wordDocTmp = WordReportDocX.LoadTemplateDocument(SavaFilesPath);
    }
    else
    {
    wordDocTmp.Paragraphs[wordDocTmp.Paragraphs.Count - 1].InsertPageBreakAfterSelf();
    wordDocTmp.InsertDocument(wordDoc);
    }
    i++;
    }
    wordDocTmp.SaveAs(SavaFilesPath);
    Document Doc = new Document(SavaFilesPath);

    //Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(Doc);

    //4/5 写入表格结束标记
    sbTable.AppendLine("</table>");

    //builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    //Doc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);

    //DocumentBuilder temdocBuilder = new DocumentBuilder(Doc);


    //var temParas = temdocBuilder.Document.Sections[1].Body.Paragraphs;
    //int intIndexer = 0;
    //foreach (Paragraph para in temParas)
    //{

    // // 在Paragraphs的第2个节点处
    // temdocBuilder.Document.Sections[0].Body.InsertAfter(para.Clone(true), temdocBuilder.Document.Sections[0].Body.Paragraphs[12 + intIndexer - 1].NextSibling);
    // intIndexer++;
    //}
    //temdocBuilder.Document.Sections.RemoveAt(1);


    //5/5 通过书签插入到指定位置
    bool isExistBook;
    isExistBook = builder.MoveToBookmark("table");
    if (isExistBook)
    { builder.InsertHtml("" + sbTable); }

    if (String.IsNullOrEmpty(pic20))
    { }
    else
    {
    bool isExistPicBook = builder.MoveToBookmark("PIC20");
    string picpath = Server.MapPath("~/Picture/ESignaturePic/" + pic20);

    if (isExistPicBook)
    {
    builder.InsertImage(picpath, 45, 25);
    }

    }


    Doc.Save(SavaFilesPath, SaveFormat.Docx);
    WindowWord.Hidden = false;
    PageContext.RegisterStartupScript(WindowWord.GetHidePostBackReference());
    return;

    }

  • 相关阅读:
    SQL Server OPENQUERY使用
    VS2013程序打包报 ISEXP : error -****: An error occurred streaming
    IIS重新注册
    c#之反射(Reflection)
    c#之Lambda表达式
    c#之委托事件(DelegateEvent)
    c#之委托详解(Delegate)
    git将文件托管到github上遇到的问题
    c#之泛型详解(Generic)
    linux下操作memcache的操作命令
  • 原文地址:https://www.cnblogs.com/sanshengshitouhua/p/14440050.html
Copyright © 2020-2023  润新知