asp.net 将DataTable中的数据导出到Excel并下载方法

我上一篇文章介绍了Excel导入到DataTable的方法,总觉得少些什么,这篇我就将DataTable

        中的数据导出到Excel并提供下载的方法记录下来。

        调用如下:

        CreateExcel(dtexcel, "application/ms-excel", excel);

        方法如下:

        /// <summary>
        /// DataTable中的数据导出到Excel并下载
        /// </summary>
        /// <param name="dt">要导出的DataTable</param>
        /// <param name="FileType">类型</param>
        /// <param name="FileName">Excel的文件名</param>
        public void CreateExcel(DataTable dt, string FileType, string FileName)
        {
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.Buffer = true;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
            Response.ContentType = FileType;
            string colHeaders = string.Empty;
            string ls_item = string.Empty;
            DataRow[] myRow = dt.Select();
            int i = 0;
            int cl = dt.Columns.Count;
            foreach (DataRow row in myRow)
            {
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }
                }
                Response.Output.Write(ls_item);
                ls_item = string.Empty;
            }
            Response.Output.Flush();
            Response.End();
        }


asp.net 将Excel中某个工作簿的数据导入到DataTable方法

       最近在网上看了几篇将Excel中某个工作簿的数据导入到DataTable的文章,自己总结了一套最实用的方法。

       这里需要注意的是:从Excel的导入到DataTable时,Excel的第一行数据会导入成DataTable的字段,所以

       Excel的第一行最好可以为空或者标识数据。

       添加引用:

       using System.Data.OleDb;

       using System.IO;

       调用如下:

       DataTable dt = GetExcelData("D:\\Data.xls", "sheet1");

       方法如下:

        /// <summary>
        /// 获取指定路径、指定工作簿名称的Excel数据
        /// </summary>
        /// <param name="FilePath">文件存储路径</param>
        /// <param name="WorkSheetName">工作簿名称</param>
        /// <returns>如果争取找到了数据会返回一个完整的Table,否则返回异常</returns>
        public DataTable GetExcelData(string FilePath, string WorkSheetName)
        {
            DataTable dtExcel = new DataTable();
            OleDbConnection con = new OleDbConnection(GetExcelConnection(FilePath));
            OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [" + WorkSheetName + "$]", con);
            //读取
            con.Open();
            adapter.FillSchema(dtExcel, SchemaType.Mapped);
            adapter.Fill(dtExcel);
            con.Close();
            dtExcel.TableName = WorkSheetName;
            //返回
            return dtExcel;
        }

        /// <summary>
        /// 获取链接字符串
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <returns></returns>
        public string GetExcelConnection(string strFilePath)
        {
            if (!File.Exists(strFilePath))
            {
                throw new Exception("指定的Excel文件不存在!");
            }
            return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";Extended properties=\"Excel 8.0;Imex=1;HDR=Yes;\"";
            //@"Provider=Microsoft.Jet.OLEDB.4.0;" +
            //@"Data Source=" + strFilePath + ";" +
            //@"Extended Properties=" + Convert.ToChar(34).ToString() +
            //@"Excel 8.0;" + "Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
        }


没有更多推荐了,返回首页