2015-02-07 130 views
0

我有這樣的代碼,我不知道如何改變標籤的字體大小和顏色,應在文本file.Here去是我的代碼:改變字體大小和顏色aspx.net

  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); 
          } 
+0

是你的'.txt'文件嗎? – prog1011 2015-02-07 09:00:20

+0

是............ – buba 2015-02-07 09:13:19

+0

string path = @「c:\ WebSite \ restourant.txt」; – buba 2015-02-07 09:14:15

回答

0

try代碼像下面

  • 你有下面的代碼更改爲根據您的要求。 (這僅僅是一個代碼,你可以實現你的代碼的方式。)
  • .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); 
        } 
    
0

注:文本文件(如果你的意思是擴展名爲.txt,在記事本 例如打開文件)沒有這樣的事情,如字體,顏色或等等

  • 您可以申請colorfont-style使用Attributes如下。

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

但是,上面的代碼 - 它不會使反映您.txt文件。

所以 - 你需要是 更具體的:你的意思是什麼文件格式; HTML可能很簡單, 可能使用或CSS屬性;通過使用RichTextBox控件 可以實現RTF;設置文本,然後操縱字體等 (類似下圖):

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 

希望這有助於

+0

我只需要以不同的大小,字體和顏色打印出空白的某種文件格式的標籤,以txt或xml格式提供 – buba 2015-02-07 10:00:33

+0

@buba - 如果你想用顏色打印這個標籤,你可以使用'window.print ()'的Javascript。 – prog1011 2015-02-07 10:03:31

+0

對不起,我想告訴寫在某種文件格式不打印 – buba 2015-02-07 10:07:15

相關問題