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();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM