iT邦幫忙

0

[C#] asp.net 匯出word套表問題

  • 分享至 

  • xImage

大家好,小弟程式新手,看了網路上部落格學習"http://deepgreat02.blogspot.com/2016/03/20160302.html"
目前照著範例嘗試,卻出現texbox問題,一直找不到哪裡錯,所以想請教各位先進。
嚴重性 程式碼 說明 專案 檔案 行 隱藏項目狀態
錯誤 CS0103 名稱 'TextBox1' 不存在於目前的內容中 WaterWebApplication C:\Users\pipi\source\repos\WaterWebApplication\WaterWebApplication\WebForm1.aspx.cs 82 作用中

謝謝大家幫忙~~
程式碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using System.IO;

namespace Darkthread.OpenXml
{
    /// <summary>
    /// Ver 1.0 By Jeffrey Lee, 2009-07-29
    /// </summary>
public class DocxHelper
{
    /// <summary>
    /// Replace the parser tags in docx document
    /// </summary>
    /// <param name="oxp">OpenXmlPart object</param>
    /// <param name="dct">Dictionary contains parser tags to replace</param>

    private static void parse(OpenXmlPart oxp, Dictionary<string, string> dct)
    {
        string xmlString = null;
        using (StreamReader sr = new StreamReader(oxp.GetStream()))
        { xmlString = sr.ReadToEnd(); }
        foreach (string key in dct.Keys)
            xmlString = xmlString.Replace("[$" + key + "$]", dct[key]);
        using (StreamWriter sw = new StreamWriter(oxp.GetStream(FileMode.Create)))
        { sw.Write(xmlString); }
    }

    /// <summary>
    /// Parse template file and replace all parser tags, return the binary content of
    /// new docx file.
    /// </summary>
    /// <param name="templateFile">template file path</param>
    /// <param name="dct">a Dictionary containing parser tags and values</param>
    /// <returns></returns>

    public static byte[] MakeDocx(string templateFile, Dictionary<string, string> dct)
    {
        string tempFile = Path.GetTempPath() + ".docx";
        File.Copy(templateFile, tempFile);

        using (WordprocessingDocument wd = WordprocessingDocument.Open(tempFile, true))
        {
            //Replace document body
            parse(wd.MainDocumentPart, dct);
            foreach (HeaderPart hp in wd.MainDocumentPart.HeaderParts)
                parse(hp, dct);
            foreach (FooterPart fp in wd.MainDocumentPart.FooterParts)
                parse(fp, dct);
        }

        byte[] buff = File.ReadAllBytes(tempFile);
        File.Delete(tempFile);
        return buff;
    }

}
}
public partial class WebForm1 : System.Web.UI.Page
    {

    protected void Page_Load(object sender, EventArgs e)
        {
        TextBox2.Text = DateTime.Now.ToString();

         }

         
        protected void Button1_Click(object sender, EventArgs e)
        {
            string template = Server.MapPath("Notice.docx");

                Dictionary<string, string> dct = new Dictionary<string, string>()
            { { "today", DateTime.Today.ToString("yyyy-MM-dd") },
              { "name", TextBox1.Text },
              { "addr", TextBox2.Text },
            };

                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("content-disposition", "attachment;filename=Notice.docx");
                Response.BinaryWrite(
                    Darkthread.OpenXml.DocxHelper.MakeDocx(
                        Server.MapPath("Notice.docx"),
                        dct)
                );
                Response.End();
            }
        }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WaterWebApplication.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <p>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>第一個</asp:ListItem>
                <asp:ListItem>第二個</asp:ListItem>
            </asp:DropDownList>
        </p>
        <p>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" style="height: 32px" />
        </p>
    </form>
</body>
</html>

fillano iT邦超人 1 級 ‧ 2020-02-24 09:54:33 檢舉
你檢查一下程式碼,通常public partial class WebForm1 : System.Web.UI.Page外面會有namespace的,你檢查一下到底應該是什麼,我猜是「WaterWebApplication」。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
fillano
iT邦超人 1 級 ‧ 2020-02-24 10:11:44
最佳解答

還是回答好了:

  1. 點選方案總管中WebForm1.aspx左側的三角形,你會看到兩個檔案
  2. 打開WebFrom1.aspx.designer.cs,看看他用的namespace是什麼
  3. 你程式public partial class WebForm1 : System.Web.UI.Page外面要用這個namespace包起來

因為你頁面上用的component是定義在這裡,如果namespace不一致的話,當然找不到。其實你查一下:
https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/compiler-messages/cs0103
就可能會有線索。

我要發表回答

立即登入回答