從伺服器下載datatable到本地,有多種處理方式,下麵介紹三種。 方式一,將datatable轉為txt下載。 步驟: 1.將datatable內容下載到伺服器txt中 2.將伺服器的txt下載到本地來 3.刪除伺服器上的txt 方式二,datatable綁定到控制項GridView後下載 步驟: ...
從伺服器下載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() == " " ? "" : 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(); }