3

在gridview之外的按钮单击时,我有几个在gridview之外的文本框和一个模板化的项目gridview。我想将数据保存到数据库表中并生成一个word文档。

在word文档中。数据将显示类似这样的内容

PO 编号:123 公司名称:xyz 订单编号:12345

项目 数量 描述 1 2 test1 2 4 test2

<asp:Label ID="lblPONumber" runat="server" Text="PO Number"></asp:Label>
<asp:TextBox ID="txtPONumber" runat="server"></asp:TextBox>

<asp:Label ID="lblCompanyName" runat="server" Text="Company Name"></asp:Label>
<asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox>

<asp:Label ID="lblOrderNo" runat="server" Text="Order Number"></asp:Label>
<asp:TextBox ID="txtOrderNo" runat="server"></asp:TextBox>

<asp:GridView ID="gvOrders" runat="server" OnRowDataBound="gvOrder_RowDataBound">
 <Columns>
   <asp:TemplateField HeaderText="Line Item">
    <ItemTemplate>
      <asp:TextBox ID="txtItem" runat="server" CssClass="Gridtextboxes" Width="150px" Text='<% #   Eval("Item")%>'></asp:TextBox></ItemTemplate>
    </asp:TemplateField>
   <asp:TemplateField HeaderText="Return Quantity">
  <ItemTemplate>
    <asp:TextBox ID="txtQuantity" runat="server" CssClass="gridt" Width="150px" Text='<% # Eval("Quantity")%>'></asp:TextBox></ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Product Description">
   <ItemTemplate>
    <asp:TextBox ID="txtProductDescription" runat="server" CssClass="Gridtextboxes" Width="150px" Text='<% # Eval("ProductDescription")%>'></asp:TextBox></ItemTemplate>
   </asp:TemplateField>
  <asp:TemplateField HeaderText="Action">
    <ItemTemplate>
       <center>
         <asp:ImageButton ID="btnDelete" runat="server" Text="Delete" ToolTip="Remove" ImageUrl="Images/close.png" Height="20px" Width="20px" /></center>
    </ItemTemplate>
  </asp:TemplateField>
 </Columns>

 <asp:Button ID="btnSave" runat="server" Text="Save" Width="110px" OnClick="btnSave_OnClick" />

在数据库中插入数据没问题,我已经完成了,我已经创建了一个 word 文档。模板是这样的。

订单号:#PONumber#

发票号码:#InvoiceNumber#

订单项:#LineItem#

退货数量:#ReturnQuantity#

产品描述:#ProductDescription#

有什么方法可以用我的 aspx 页面数据重播 word doc #Data#?

4

3 回答 3

1

我以 Aspose 发誓。他们拥有出色的工具来处理 Office 文档和 PDF 文件。虽然它们非常昂贵,但每次我使用这些工具时,我都节省了数周的工作时间,所以最终它会变得更便宜。

于 2013-09-22T09:43:01.217 回答
1

无需使用任何第三方。

首先您需要将所有数据从网格和文本框导入到数据库中。

protected void btnAddCopy_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < gvTrip.Rows.Count; i++)
                {
                        string strTripDate = DateValidator.ConvertToMMDDYYYY(txtCopyTripDt.Text).ToString();
                        string strRouteSlcn = Convert.ToString(((Label)gvTrip.Rows[i].FindControl("lblgRouteSlcn2")).Text.Trim());
                        string strTripStartTime = Convert.ToString(((Label)gvTrip.Rows[i].FindControl("lblgTripStartTime")).Text.Trim());
                        string strTripEndTime = Convert.ToString(((Label)gvTrip.Rows[i].FindControl("lblgTripEndTime")).Text.Trim());
                        objMasterTransport.**insertTripDetail**(strTripDate, strRouteSlcn, strTripStartTime, strTripEndTime);
objMasterTransport.**YourFunctionForGenWordFile**(strTripDate, strRouteSlcn, strTripStartTime, strTripEndTime);
                    txtCopyTripDt.Text = null;
                    gvTrip.DataBind();
                    BindData();
       }
    }

insertTripDetail 是我在某个地方的函数。我删除了一些代码。

生成word文件的例子是(来自网络)

protected void ExportToWord(object sender, EventArgs e)
{

    //Get the data from database into datatable
    string strQuery = "select CustomerID, ContactName, City, PostalCode" +
                      " from customers";
    SqlCommand cmd = new SqlCommand(strQuery);
    DataTable dt = GetData(cmd);

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
        "attachment;filename=DataTable.doc");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-word ";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.RenderControl(hw);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}

您可以从数据库中获取数据或将其存储在这些变量中(strTripDate、strRouteSlcn、strTripStartTime、strTripEndTime)

这是 excel 更新的代码链接 http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/

于 2013-09-26T05:15:25.247 回答
0

我从这个链接找到了解决方案

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using Microsoft.Office.Interop.Word;

 protected void btnSubmit_OnClick(object sender, EventArgs e)
    {
        try
        {
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc";
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

            Word.Paragraph oParag;
            oParag = oDoc.Content.Paragraphs.Add(ref oMissing);
            oParag.Range.Text = "Return Order";
            oParag.Range.Font.Bold = 2;
            oParag.Format.SpaceAfter = 30;
            oParag.Range.InsertParagraphAfter();

            Word.Paragraph oPara1;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara1.Range.Text = "Date: " + txtDate.Text;
            oPara1.Range.Font.Bold = 0;
            oPara1.Format.SpaceAfter = 24;
            oPara1.Range.InsertParagraphAfter();

            Word.Paragraph oPara2;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara2.Range.Text = "PO Number: " + txtPONumber.Text;
            oPara2.Range.Font.Bold = 0;
            oPara2.Format.SpaceAfter = 24;
            oPara2.Range.InsertParagraphAfter();

            Word.Paragraph oPara3;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara3.Range.Text = "Invoice Number: " + txtnvoiceNo.Text;
            oPara3.Range.Font.Bold = 0;
            oPara3.Format.SpaceAfter = 24;
            oPara3.Range.InsertParagraphAfter();

            Word.Paragraph oPara4;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara4.Range.Text = "Company Name or Dealer Name: " + txtCompanyName.Text;
            oPara4.Range.Font.Bold = 0;
            oPara4.Format.SpaceAfter = 24;
            oPara4.Range.InsertParagraphAfter();

            Word.Paragraph oPara5;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara5 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara5.Range.Text = "Order Number: " + txtOrderNo.Text;
            oPara5.Range.Font.Bold = 0;
            oPara5.Format.SpaceAfter = 24;
            oPara5.Range.InsertParagraphAfter();

            Word.Paragraph oPara6;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara6 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara6.Range.Text = "Contact Person to Return: " + txtReturnPerson.Text;
            oPara6.Range.Font.Bold = 0;
            oPara6.Format.SpaceAfter = 24;
            oPara6.Range.InsertParagraphAfter();

            Word.Paragraph oPara7;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara7 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara7.Range.Text = "Email: " + txtEmail.Text;
            oPara7.Range.Font.Bold = 0;
            oPara7.Format.SpaceAfter = 24;
            oPara7.Range.InsertParagraphAfter();

            //inserting table
            Word.Table oTable;
            int iTblRowCount = gvReturnOrders.Rows.Count + 1;
            Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oTable = oDoc.Tables.Add(wrdRng, iTblRowCount, 3, ref oMissing, ref oMissing);
            oTable.Range.ParagraphFormat.SpaceAfter = 6;

            oTable.Rows[1].Cells[1].Range.Text = "Line Item";
            oTable.Rows[1].Cells[2].Range.Text = "Return Quantity";
            oTable.Rows[1].Cells[3].Range.Text = "Product Description";
            oTable.Rows[1].Cells[1].Range.Bold = 1;
            oTable.Rows[1].Cells[2].Range.Bold = 1;
            oTable.Rows[1].Cells[3].Range.Bold = 1;

            int iRowCount, iCount = 2;
            int rowIndex = 0;
            for (iRowCount = 1; iRowCount <= gvReturnOrders.Rows.Count; iRowCount++)
            {
                TextBox txtboxLineItems = (TextBox)gvReturnOrders.Rows[rowIndex].Cells[1].FindControl("txtLineItem");
                TextBox txtBoxQty = (TextBox)gvReturnOrders.Rows[rowIndex].Cells[2].FindControl("txtReturnQuantity");
                TextBox txtBoxproductDescription = (TextBox)gvReturnOrders.Rows[rowIndex].Cells[3].FindControl("txtProductDescription");
                oTable.Rows[iCount].Cells[1].Range.Text = txtboxLineItems.Text;
                oTable.Rows[iCount].Cells[2].Range.Text = txtBoxQty.Text;
                oTable.Rows[iCount].Cells[3].Range.Text = txtBoxproductDescription.Text;
                iCount++;
                rowIndex++;
            }
            //var myUniqueFileName = string.Format(@"{0}.doc", Guid.NewGuid()); // for Unique Id
            string fileName = "C:\\ReturnOrder.doc";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
                oDoc.SaveAs2("C:\\ReturnOrder.doc");
                lblMsg.Visible = true;
                lblMsg.Text = "Successful";
            }
            else
            {
                oDoc.SaveAs2("C:\\ReturnOrder.doc");
                lblMsg.Visible = true;
                lblMsg.Text = "Successful";
            }
            oDoc.Close();
            oWord.Quit();

        }
        catch (Exception ex)
        {

        }
    }
于 2013-09-19T12:51:44.583 回答