C# DataTable下载

从服务器下载datatable到本地,有多种处理方式,下面介绍三种。

方式一,将datatable转为txt下载。

步骤:

1.将datatable内容下载到服务器txt中

2.将服务器的txt下载到本地来

3.删除服务器上的txt

方式二,datatable绑定到控件GridView后下载

步骤:

1.关闭控件分页功能并绑定数据到控件

2.下载控件内容到本地

3.打开控件分页功能并重新绑定数据

方式三,datatable转为Excel下载

步骤:

1.将datatable内容下载到服务器Excel中

2.将服务器中的Excel下载到本地

3.删除服务器上的Excel

如果大家有其他好的方式,不妨分享下,最好有源码,哈哈哈~~~~

//datatable转为txt 
public void DataTableToTxt(string filePath,DataTable ds) {
            FileStream fs = new FileStream(filePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            for (int j=0; j < ds.Columns.Count; j++) {
                sw.Write(ds.Columns[j].ColumnName);
                sw.Write("\t");
            }
            sw.Write("\r\n");
                for (int i = 0; i < ds.Rows.Count; i++)
                {
                    for (int j = 0; j < ds.Columns.Count; j++)
                    {
                        sw.Write(ds.Rows[i][j].ToString() == "&nbsp;" ? "" : ds.Rows[i][j].ToString());
                        sw.Write("\t");
                    }
                    sw.Write("\r\n");
                }
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }
//下载控件内容
public void GridviewToExcel(Control control, string FileType, string FileName)
        {
            HttpContext.Current.Response.Charset = "GB2312";//设置了类型为中文防止乱码的出现 
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//注意编码
            HttpContext.Current.Response.AppendHeader("Content-Disposition",
                 "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());//将http头添加到输出流
            HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 

            control.Page.EnableViewState = false;//服务器端只做出一次响应
            StringWriter tw = new StringWriter();//实现将信息写入字符串(下面的信息会写到这里)
            HtmlTextWriter hw = new HtmlTextWriter(tw);//用于将标记字符和文本写入到ASP.NET服务器控件输出流
            control.RenderControl(hw);//将服务器控件的内容输出到所提供的 HtmlTextWriter 对象中
       

            HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            HttpContext.Current.Response.End();
        }
//调用
GridviewToExcel(grd, "application/vnd.ms-excel.numberformat:@", "xx.xls");
//datatable 转excel
public void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
        {
            if (tmpDataTable == null)

                return;
            int rowNum = tmpDataTable.Rows.Count;
            int columnNum = tmpDataTable.Columns.Count;
            int rowIndex = 1;
            int columnIndex = 0;
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.DefaultFilePath = "";
            xlApp.DisplayAlerts = true;
            xlApp.SheetsInNewWorkbook = 1;
            Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
            Microsoft.Office.Interop.Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.ActiveSheet;
            //将DataTable的列名导入Excel表第一行
            foreach (DataColumn dc in tmpDataTable.Columns)
            {
                columnIndex++;
                xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;

            }

            //将DataTable中的数据导入Excel中
            xlSheet.Range[xlSheet.Cells[1, 1], xlSheet.Cells[rowNum + 1, columnNum+1]].NumberFormatLocal = "@";//将格式设置成文本
            for (int i = 0; i < rowNum; i++)
            {              
                rowIndex++;
                columnIndex = 0;
                for (int j = 0; j < columnNum; j++)
                {
                    columnIndex++;
                    xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();

                }

            }    
            //xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8));
            xlBook.SaveCopyAs(strFileName);
        }
DataTabletoExcel(ds, Server.MapPath("") + "\\xx.xlsx");
//文件下载 参数 文件名  文件路径 
public void DownFileToLocal(string FileName,string FilePath) {
            FileInfo fileInfo = new FileInfo(FilePath);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
            HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            HttpContext.Current.Response.WriteFile(fileInfo.FullName);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }

 

posted @ 2019-04-18 16:05  停车坐爱枫林晚  阅读(1002)  评论(2编辑  收藏  举报