当前位置:首页经验技巧Excel经验excel知识

EXCEL和sw如何画凸轮

2026-01-20 15:19:31

1.C#如何实现datagridview写入excel

导出数据为Excel文件在开发项目时比较常见的一种需求 。

以前对于数据量较小的情况使用 Microsoft.Office.Interop.Excel.Workbooks相关类,编写起来也比较麻烦,对于数据量较大的情况,在此与大家共享使用SteamWriter类输出Excel文件的方法。经过具体测试,通过在程序中使用多线程配置该方法,导出300000行+17列的约130M的数据需要31秒左右。

[csharp] view plaincopyprint? 1. ///

2. /// 导出文件,使用文件流。该方法使用的数据源为DataTable,导出的Excel文件没有具体的样式。

3. ///

4. /// 5. public static string ExportToExcel(System.Data.DataTable dt, string path) 6. { 7. KillSpecialExcel(); 8. string result = string.Empty; 9. try 10. { 11. // 实例化流对象,以特定的编码向流中写入字符。 12. StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312")); 13. 14. StringBuilder sb = new StringBuilder(); 15. for (int k = 0; k < dt.Columns.Count; k++) 16. { 17. // 添加列名称 18. sb.Append(dt.Columns[k].ColumnName.ToString() + "\t"); 19. } 20. sb.Append(Environment.NewLine); 21. // 添加行数据 22. for (int i = 0; i < dt.Rows.Count; i++) 23. { 24. DataRow row = dt.Rows[i]; 25. for (int j = 0; j < dt.Columns.Count; j++) 26. { 27. // 根据列数追加行数据 28. sb.Append(row[j].ToString() + "\t"); 29. } 30. sb.Append(Environment.NewLine); 31. } 32. sw.Write(sb.ToString()); 33. sw.Flush(); 34. sw.Close(); 35. sw.Dispose(); 36. 37. // 导出成功后打开 38. //System.Diagnostics.Process.Start(path); 39. } 40. catch (Exception) 41. { 42. result = "请保存或关闭可能已打开的Excel文件"; 43. } 44. finally 45. { 46. dt.Dispose(); 47. } 48. return result; 49.} 50./// 51./// 结束进程 52./// 53.private static void KillSpecialExcel() 54.{ 55. foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL")) 56. { 57. if (!theProc.HasExited) 58. { 59. bool b = theProc.CloseMainWindow(); 60. if (b == false) 61. { 62. theProc.Kill(); 63. } 64. theProc.Close(); 65. } 66. } 67.} B/S 导出: // 保存错误信息 GridView gv = new GridView(); gv.DataSource = dtError; gv.DataBind(); gv.Attributes.Add("style", "vnd.ms-excel.numberformat:@"); HttpResponse hResponse = this.Response; string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd"); hResponse.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls"); hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); hResponse.ContentType = "application/ms-excel"; this.EnableViewState = false; StringWriter tw = new StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); gv.RenderControl(hw); hResponse.Write(tw); hResponse.End();。

2.如何通过aspx页面生成excel

前台代码:

<asp:GridView ID="GridView1" runat="server" >

</asp:GridView>

<br />

<asp:Button ID="btnExportToExcel" runat="server"

Text="导出数据到Excel" />

后台代码:

/// <summary>

/// 将GridView的数据输出到Excel文件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void btnExportToExcel_Click(object sender, EventArgs e)

{

if (GridView1.Rows.Count == 0)

{

return;

}

Response.ClearContent();

Response.ContentEncoding = Encoding.Default;

Response.AddHeader("content-disposition", "attatchment;filename=" + HttpUtility.UrlEncode("我的Excel.xls", Encoding.UTF8));

Response.ContentType = "application/ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();

}

//必须重载此方法,以便支持上面的导出操作

public override void (Control control)

{

}


免责声明:本站信息来自网络收集及网友投稿,仅供参考,如果有错误请反馈给我们更正,对文中内容的真实性和完整性本站不提供任何保证,不承但任何责任,谢谢您的合作。
版权所有:五学知识网 Copyright © 2015-2026 www.z8000w.com. All Rights Reserved .