日期:2014-05-18  浏览次数:20498 次

.net数据导入导出Excel
.net中怎么实现把数据从数据库中导出到Excel中,和把数据从Excel导入到数据库(sqlserver)中,谁能介绍种比较好的方法,实现数据快速的导入导出。

------解决方案--------------------
导出到Excel

C# code

public class GridViewToExcel
    {
        HttpResponse Response = null;
        public GridViewToExcel(HttpResponse response)
        {
            Response = response;
        }

        /// <summary>
        /// 将GridView里的数据导入到Excel文件中,并另存文档
        /// </summary>
        /// <param name="FileName">文件名称</param>
        /// <param name="GridViewTeacher">要导出数据的GridView</param>
        public void ExportToExcel(string FileName, GridView gridview)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = "application/ms-excel";//导出excel文件
            //this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            gridview.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
    }

------解决方案--------------------
探讨
C# code

private void btnOk_Click(object sender, EventArgs e)
{
if (this.txtFilePath.Text.Trim() == "")
{
MessageBox.Show("请选择要导入的EXCEL文件!", "友情提示……

------解决方案--------------------
如果你对导出Excel格式没要求,就按上面的来,如果有要求就按我的博客写的
http://www.cnblogs.com/wuxing