用c#代码实现txt文件另存为html文件(是另存!!)

非常急,非常感谢!!... 非常急,非常感谢!! 展开
 我来答
百度网友08f6db01a
2013-07-17 · TA获得超过257个赞
知道小有建树答主
回答量:345
采纳率:50%
帮助的人:109万
展开全部

C#读取TXT文件之HTML的文件:

  1. ﹤body﹥  
  2.    ﹤form id="form1" runat="server"﹥  
  3.    ﹤div﹥  
  4.         ﹤/div﹥  
  5.        ﹤table border="0" cellpadding="0"  
  6. cellspacing="0" style="width: 603px; height: 148px"﹥  
  7. ﹤tr﹥  
  8.    ﹤td style="width: 100px"﹥  
  9.        序号:﹤asp:TextBox ID="TextBox1" runat="server"﹥  
  10. ﹤/asp:TextBox﹥﹤/td﹥  
  11.    ﹤td style="width: 100px"﹥  
  12.        姓名:﹤asp:TextBox ID="TextBox2" runat="server"﹥  
  13. ﹤/asp:TextBox﹥﹤/td﹥  
  14.    ﹤td style="width: 100px"﹥  
  15.        出生日期:﹤asp:TextBox ID="TextBox3" runat="server"﹥  
  16. ﹤/asp:TextBox﹥﹤/td﹥  
  17. ﹤/tr﹥  //C#读取TXT文件
  18. ﹤tr﹥  
  19.    ﹤td style="width: 100px"﹥  
  20.        ﹤asp:FileUpload ID="FileUpload2" runat="server" /﹥  
  21. ﹤/td﹥  
  22.    ﹤td style="width: 100px"﹥  
  23.        ﹤asp:Button ID="Button1" runat="server"  
  24. OnClick="Button1_Click" Text="导入" /﹥  
  25.        ﹤asp:Button ID="Button2" runat="server"  
  26. OnClick="Button2_Click" Text="查询" /﹥﹤/td﹥  
  27.    ﹤td style="width: 100px"﹥  
  28.    ﹤/td﹥  
  29. ﹤/tr﹥  
  30.        ﹤/table﹥  
  31.    ﹤/form﹥  
  32. ﹤/body﹥

C#读取TXT文件的C#代码:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.IO;  
  12. using System.Data.SqlClient;  
  13. public partial class _Default : System.Web.UI.Page  
  14. {  
  15.    string id1, name, birthday, str,sfz,sex;  
  16.    SqlConnection con = new SqlConnection();  
  17.    SqlDataReader dr = null;  
  18.    //int i,count= 1;
  19.    protected void Page_Load(object sender, EventArgs e)  
  20.    {  
  21.    }  //C#读取TXT文件
  22.    private void LeadInsert()  
  23.    {  
  24.        try
  25.        {  
  26. string path = FileUpload2.PostedFile.FileName; //定义上传路径
  27. StreamReader smRead = new StreamReader(path,  
  28. System.Text.Encoding.Default); //设置路径
  29. string line;  
  30. while ((line = smRead.ReadLine()) != null)  
  31. {  
  32.    string[] arrStr = line.Split('|');     //分割符 “|”
  33.    id1 = arrStr[0].ToString();  
  34.    name = arrStr[1].ToString();  
  35.    sfz = arrStr[2].ToString();  
  36.    //birthday = arrStr[3].ToString();
  37.    //sex = arrStr[4].ToString();
  38.    if (Convert.ToInt32(sfz.Length.ToString())==18)  
  39.    {  
  40.        birthday = sfz.Substring(6, 8);  
  41.        DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  42.        birthday = mydt.ToString("yyyy-MM-dd");  
  43.    }  
  44.    if (Convert.ToInt32(sfz.Length.ToString())== 15)  
  45.    {  
  46.        birthday = "19"+sfz.Substring(6, 6).ToString();  
  47.        DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  48.        birthday = mydt.ToString("yyyy-MM-dd");  
  49.    }  
  50.    if (Convert.ToInt32(sfz.Length.ToString()) == 18)  
  51.    {  
  52.        if (((Convert.ToInt16(sfz.Substring(16, 1).ToString())) % 2 == 0))  
  53.           {  
  54. sex = "0";  
  55. }  
  56. else               //C#读取TXT文件
  57. {  
  58.   sex = "1";  
  59. }  
  60.        }  
  61.        if (Convert.ToInt32(sfz.Length.ToString()) == 15)  
  62.        {  
  63. if ((( Convert.ToInt16(sfz.Substring(13, 1).ToString())) % 2 == 0))  
  64. {  
  65.    sex= "0";  
  66. }  
  67. else
  68. {  
  69.   sex = "1";  
  70. }  
  71.        }  
  72.    string ConnStr = "server=localhost;uid=sa;pwd=sa;database=GAC";  
  73.    con.ConnectionString = ConnStr;  
  74.    con.Open();  
  75.    SqlCommand cmd = new SqlCommand();  
  76.    cmd.CommandText = "insert into txt (name,birthday,sex,sfz)  
  77. VALUES (@name,@birthday,@sex,@sfz)";  
  78.    cmd.Connection = con;  
  79.    cmd.Parameters.Clear();  
  80.    //cmd.Parameters.Add("@id1", SqlDbType.Int);
  81.    //cmd.Parameters["@id1"].Value = Convert.ToInt32(id1);
  82.    cmd.Parameters.Add("@name", SqlDbType.VarChar, 50);  
  83.    cmd.Parameters["@name"].Value = name.ToString();  
  84.    cmd.Parameters.Add("@sfz", SqlDbType.VarChar, 20);  
  85.    cmd.Parameters["@sfz"].Value = sfz.ToString();  
  86.    cmd.Parameters.Add("@birthday", SqlDbType.VarChar, 50);  
  87.    if (birthday.Length ﹥ 0)  
  88.    {  
  89.        cmd.Parameters["@birthday"].Value = birthday.ToString();  
  90.    }  
  91.    else {  
  92.        cmd.Parameters["@birthday" ].Value = DBNull.Value;  
  93.     }  
  94.    cmd.Parameters.Add("@sex", SqlDbType.Char, 10);  
  95.    if (sex.Length ﹥ 0)  
  96.    {  
  97.        cmd.Parameters["@sex"].Value = Convert.ToInt16(sex.ToString());  
  98.    }  
  99.    else
  100.    {  //C#读取TXT文件
  101.        cmd.Parameters["@sex"].Value = DBNull.Value;  
  102.    }  
  103.    cmd.ExecuteNonQuery();  
  104.    con.Close();  
  105.          }  
  106.        }  
  107.        catch (Exception ee) { }  
  108.        finally {  
  109.            
  110.        }  
  111.    }  
  112.    protected void Button1_Click(object sender, EventArgs e)  
  113.    {  
  114.        LeadInsert();  
  115.    }  
  116.    protected void Button2_Click(object sender, EventArgs e)  
  117.    {  
  118.    }  
  119. }  
  120. //C#读取TXT文件
  121. using System;  
  122. using System.Data;  
  123. using System.Configuration;  
  124. using System.Collections;  
  125. using System.Web;  
  126. using System.Web.Security;  
  127. using System.Web.UI;  
  128. using System.Web.UI.WebControls;  
  129. using System.Web.UI.WebControls.WebParts;  
  130. using System.Web.UI.HtmlControls;  
  131. using System.IO;  
  132. using System.Data.SqlClient;  
  133. public partial class _Default : System.Web.UI.Page  
  134. {  
  135.    string id1, name, birthday, str,sfz,sex;  
  136.    SqlConnection con = new SqlConnection();  
  137.    SqlDataReader dr = null;  
  138.    //int i,count= 1;
  139.    protected void Page_Load(object sender, EventArgs e)  
  140.    {  
  141.    }  
  142.    private void LeadInsert()  
  143.    {  
  144.        try
  145.        {  //C#读取TXT文件
  146. string path = FileUpload2.PostedFile.FileName;  
  147. StreamReader smRead = new StreamReader(path,  
  148. System.Text.Encoding.Default);  
  149. string line;  
  150. while ((line = smRead.ReadLine()) != null)  
  151. {  
  152.    string[] arrStr = line.Split('|');  
  153.    id1 = arrStr[0].ToString();  
  154.    name = arrStr[1].ToString();  
  155.    sfz = arrStr[2].ToString();  
  156.    //birthday = arrStr[3].ToString();
  157.    //sex = arrStr[4].ToString();
  158.    if (Convert.ToInt32(sfz.Length.ToString())==18)  
  159.    {  
  160.        birthday = sfz.Substring(6, 8);  
  161.        DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  162.        birthday = mydt.ToString("yyyy-MM-dd");  
  163.    }  
  164.    if (Convert.ToInt32(sfz.Length.ToString())== 15)  
  165.    {  
  166.        birthday = "19"+sfz.Substring(6, 6).ToString();  
  167.        DateTime mydt = DateTime.ParseExact(birthday, "yyyyMMdd", null);  
  168.        birthday = mydt.ToString("yyyy-MM-dd");  
  169.    }  
  170.    if (Convert.ToInt32(sfz.Length.ToString()) == 18)  
  171.    {  
  172.        if (((Convert.ToInt16(sfz.Substring(16, 1).ToString())) % 2 == 0))  
  173.           {  
  174. sex = "0";  
  175. }  
  176. else
  177. {  //C#读取TXT文件
  178.   sex = "1";  
  179. }  
  180.        }  
  181.        if (Convert.ToInt32(sfz.Length.ToString()) == 15)  
  182.        {  
  183. if ((( Convert.ToInt16(sfz.Substring(13, 1).ToString())) % 2 == 0))  
  184. {  
  185.    sex= "0";  
  186. }  
  187. else
  188. {  
  189.   sex = "1";  
  190. }  
  191.        }  
  192.    string ConnStr = "server=localhost;uid=sa;pwd=sa;database=GAC";  
  193.    con.ConnectionString = ConnStr;  
  194.    con.Open();  
  195.    SqlCommand cmd = new SqlCommand();  
  196.    cmd.CommandText = "insert into txt (name,birthday,sex,sfz) VALUES (@name,@birthday,@sex,@sfz)";  
  197.    cmd.Connection = con;  
  198.    cmd.Parameters.Clear();  
  199.    //cmd.Parameters.Add("@id1", SqlDbType.Int);
  200.    //cmd.Parameters["@id1"].Value = Convert.ToInt32(id1);
  201.    cmd.Parameters.Add("@name", SqlDbType.VarChar, 50);  
  202.    cmd.Parameters["@name"].Value = name.ToString();  
  203.    cmd.Parameters.Add("@sfz", SqlDbType.VarChar, 20);  
  204.    cmd.Parameters["@sfz"].Value = sfz.ToString();  
  205.    cmd.Parameters.Add("@birthday", SqlDbType.VarChar, 50);  
  206.    if (birthday.Length ﹥ 0)  
  207.    {  
  208.        cmd.Parameters["@birthday"].Value = birthday.ToString();  
  209.    }  
  210.    else {  
  211.        cmd.Parameters["@birthday" ].Value = DBNull.Value;  
  212.     }  
  213.    cmd.Parameters.Add("@sex", SqlDbType.Char, 10);  
  214.    if (sex.Length ﹥ 0)  
  215.    {  
  216.        cmd.Parameters["@sex"].Value = Convert.ToInt16(sex.ToString());  
  217.    }  
  218.    else
  219.    {  //C#读取TXT文件
  220.        cmd.Parameters["@sex"].Value = DBNull.Value;  
  221.    }  
  222.    cmd.ExecuteNonQuery();  
  223.    con.Close();  
  224.          }  
  225.        }  
  226.        catch (Exception ee) { }  
  227.        finally {  
  228.            
  229.        }  
  230.    }  
  231.    protected void Button1_Click(object sender, EventArgs e)  
  232.    {  
  233.        LeadInsert();  
  234.    }  
  235.    protected void Button2_Click(object sender, EventArgs e)  
  236.    {  
  237.    }  
  238. }

C#读取TXT文件的内容就向你介绍到这里,希望对你学习C#读取TXT文件有所帮助。

百度网友6fcf96b
2013-07-17 · TA获得超过127个赞
知道小有建树答主
回答量:120
采纳率:100%
帮助的人:83.2万
展开全部
你是想批量处理???
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式