如何将 gridview 模板化的 Item 数据保存到 DB 以及生成 word 文档? [英] How to save gridview templated Item data into DB as well as generate a word document?
问题描述
我有几个位于 gridview 之外的文本框和一个模板化项目 gridview,点击按钮时位于 gridview 之外.我想将数据保存到数据库表中并生成一个word文档.
I have few textbox which is outside of gridview and a templated item gridview, on button click which is outside of gridview. I want to save data into database table as well as generate a word document.
在word文档中.数据将显示类似这样的内容
In word doc. data will show something like this
采购订单编号:123公司名称:xyz订单号:12345
PO No: 123 Company Name: xyz Order No: 12345
商品数量说明1 2 测试12 4 测试2
Item Qty Description 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 文档.类似这样的模板.
Inserting data in DB is okay I have done with that and I have Created a word doc. template something like this.
采购订单编号:#PONumber#
PO Number: #PONumber#
发票编号:#InvoiceNumber#
Invoice Number: #InvoiceNumber#
订单项:#LineItem#
Line Item : #LineItem#
退货数量:#ReturnQuantity#
Return Quantity : #ReturnQuantity#
产品描述:#ProductDescription#
Product Description : #ProductDescription#
有什么方法可以用我的 aspx 页面数据重放 word doc #Data# 吗?
Is there any way in which I can replay word doc #Data# with my aspx page data?
推荐答案
我从这个链接中找到了解决方案
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)
{
}
}
这篇关于如何将 gridview 模板化的 Item 数据保存到 DB 以及生成 word 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!