我是C#开发的WINFORM 客户端程序呀,不是B/S模式.

解决方案 »

  1.   

    /// <summary>
            /// 以写文件的方式把DataTable数据导出到Excel
            /// </summary>
            /// <param name="dt">DataTable对象</param>
            /// <param name="strExcelFileName">Excel文件名</param>
            /// <param name="strError">out参数,返回出错信息</param>
            /// <param name="isName">是否写标题 true:需要 false:不需要</param>
            /// <param name="strFileName">标题名称</param>
            /// <returns>
            ///    -1 出错
            ///    0 成功
            /// </returns>
            public static int DataTableToExcel2(DataTable dt,
                string strExcelFileName,string strFileName,bool isName,
                out string strError)
            {
                strError = "";
                int nRet = 0;            if (dt == null)
                {
                    strError = "数据集不能为null";
                    nRet = -1;
                    return nRet;
                }
                if (strExcelFileName.Trim() == string.Empty || strExcelFileName == null)
                {
                    strError = "文件名不能为空";
                    nRet = -1;
                    return nRet;
                }
                if (isName && strFileName == string.Empty)
                {
                    strError = "标题名不能为空";
                    nRet = -1;
                    return nRet;
                }
                string strLine = "";            FileStream fs = new FileStream(strExcelFileName,
                    FileMode.Create,
                    FileAccess.Write);            StreamWriter sw = new StreamWriter(fs,
                    System.Text.Encoding.Unicode);            try
                {
                    if (isName)
                    {
                        sw.WriteLine(strFileName);
                    }                for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        strLine += dt.Columns[i].ColumnName.ToString();
                    }
                    sw.WriteLine(strLine);                for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            strLine +=  dt.Rows[i][j].ToString();
                        }
                        sw.WriteLine(strLine);
                    }
                }
                catch (Exception ex)
                {
                    strError = ex.Message;
                    return -1;
                }
                finally
                {
                    sw.Close();
                    fs.Close();
                }            return 0;
            }
      

  2.   

    http://blog.csdn.net/fangxinggood/archive/2006/04/08/655313.aspx