【问题标题】:change font size and color in StreamWriter aspx.net在 StreamWriter aspx.net 中更改字体大小和颜色
【发布时间】:2015-02-07 19:36:54
【问题描述】:

我有这个代码,但我不知道如何更改标签中的字体大小和颜色,应该放在文本文件中。这是我的代码:

            if (!File.Exists(path))
                {
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        sw.WriteLine("Artikal                     Kol    Prize  Sum");

                            if (Label53.Text == "4")
                            {

                                string artikal = Label47.Text;

                                string Kol    = Label45.Text;
                                string Prize= Label48.Text;
                                string Sum= Label54.Text;

                                sw.WriteLine(artikal);
                                sw.WriteLine(Kol    + "X                              " + Prize+ "       " + Sum);
                            }

【问题讨论】:

  • 你的文件是.txt吗?
  • 字符串路径 = @"c:\WebSite\restourant.txt";

标签: asp.net


【解决方案1】:

试试下面的代码

  • 您必须根据您的要求更改以下代码。 (这只是您可以实现代码的方式。)
  • .aspx 文件

    <asp:Button ID="btnCreateFile" Text="Create File" runat="server"/>

  • .aspx.cs 文件(代码隐藏)

    protected void btnCreateFile_Click(object sender, EventArgs e) { CreateFile(); }

        private void CreateFile()
        {
            StringBuilder strBody = new StringBuilder();
            strBody.Append(@"<html " +
            "xmlns:o='urn:schemas-microsoft-com:office:office' " +
            "xmlns:w='urn:schemas-microsoft-com:office:word'" +
            "xmlns='http://www.w3.org/TR/REC-html40'>" +
            "<head><title>Time</title>");
            strBody.Append("<!--[if gte mso 9]>" +
                                     "<xml>" +
                                     "<w:WordDocument>" +
                                     "<w:View>Print</w:View>" +
                                     "<w:Zoom>90</w:Zoom>" +
                                     "<w:DoNotOptimizeForBrowser/>" +
                                     "</w:WordDocument>" +
                                     "</xml>" +
                                     "<![endif]-->");
    
            strBody.Append("<style>" +
                                    "<!-- /* Style Definitions */" +
                                    "@page Section1" +
                                    "   {size:8.5in 11.0in; " +
                                    "   margin:1.0in 1.25in 1.0in 1.25in ; " +
                                    "   mso-header-margin:.5in; " +
                                    "   mso-footer-margin:.5in; mso-paper-source:0;}" +
                                    " div.Section1" +
                                    "   {page:Section1;}" +
                                    "-->" +
                                   "</style></head>");
    
            strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" +
                                    "<div class=Section1>" +
                                    "<h1>Time and tide wait for none</h1>" +
                                    "<p style='color:red'><I>" +
                                    DateTime.Now + "</I></p>" +
                                    "</div></body></html>");
    
            Response.AppendHeader("Content-Type", "application/msword");
            Response.AppendHeader("Content-disposition",
                                   "attachment; filename=mywosrd.doc");
            Response.Write(strBody);
        }
    

【讨论】:

    【解决方案2】:

    注意: 文本文件(如果您指的是在记事本中打开的具有 .txt 扩展名的文件 例如)没有诸如字体颜色或等之类的东西。

    • 您可以使用Attributes 申请colorfont-style,如下所示。

    Label45.Attributes["style"] = "color:red; font-weight:bold;";

    但是,上面的代码 - 它不会反映在您的 .txt 文件中。

    所以 - 你需要 更具体:你的意思是什么文件格式; HTML 会很简单, 大概使用或 CSS 属性; RTF 可以通过使用来实现 RichTextBox 控件;设置文本,然后操作字体等 (如下所示):

    richTextBox1.Text = "Some Text";
    richTextBox1.SelectAll();
    richTextBox1.SelectionFont = new Font("Times New Roman", 20);
    richTextBox1.SelectionColor = Color.Aquamarine;
    richTextBox1.SaveFile("C:\\restourant.rtf"); // or try with ".doc" file
    

    希望这会有所帮助,

    【讨论】:

    • 我只需要以空的某种文件格式打印出不同大小、字体和颜色的标签,最好是txt或xml格式
    • @buba - 如果你想用颜色打印这个标签,你可以使用 JavaScript 的 window.print()
    • 对不起,我想告诉你用某种文件格式写而不是打印
    • 我可以发送 jepg 格式的标签吗?它可以在那里反映这种变化吗?
    • 你能写一些例子如何用html做到这一点吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多