zoukankan      html  css  js  c++  java
  • C#写数据到Excel

    private void ImportToExcel(string strFileName, DataSet ds)
        {
            Stream myStream = System.IO.File.Create(strFileName);
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            string str = "";
            try
            {
                //写标题
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    if (i > 0)
                        str += "\t";
                    str += ds.Tables[0].Columns[i].ColumnName;
                }
                sw.WriteLine(str);

                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    string strTmp = "";
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        if (j > 0)
                            strTmp += "\t";
                        strTmp += ds.Tables[0].Rows[i][j].ToString();
                    }
                    sw.WriteLine(strTmp);
                }
                sw.Close();
                myStream.Close();
            }
            catch (Exception ex)
            { }
            finally
            {
                sw.Close();
                myStream.Close();
            }

        }

  • 相关阅读:
    TreeMap<K,V>类
    2020-3-7学习地图
    Thread类
    Redis-Windows中注册Redis后台守护进程
    Redsi-(error) NOAUTH Authentication required.
    2020-3-6学习地图
    Collection<E>接口
    Map<K,V>接口
    Set接口
    List类
  • 原文地址:https://www.cnblogs.com/jacker1979/p/1531832.html
Copyright © 2011-2022 走看看