exceltoaccess.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Data.OleDb;
  5. using System.Configuration;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. /// <summary>
  13. /// exceltoaccess 的摘要说明
  14. /// </summary>
  15. public class exceltoaccess
  16. {
  17. public exceltoaccess()
  18. {
  19. //
  20. // TODO: 在此处添加构造函数逻辑
  21. //
  22. }
  23. string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~\\App_Data\\ZY.mdb");
  24. public void gettable(string conn, DropDownList DropDownList1)
  25. {
  26. OleDbConnection connect = new OleDbConnection(conn);
  27. connect.Open();
  28. DataTable schemaTable = connect.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  29. for (int i = 0; i <= schemaTable.Rows.Count - 1; i++)
  30. {
  31. if (schemaTable.Rows[i].ItemArray[3].ToString() == "TABLE")
  32. { DropDownList1.Items.Add(schemaTable.Rows[i].ItemArray[2].ToString()); }
  33. }
  34. }
  35. public DataTable getds(string conn, string Sql)
  36. {
  37. OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, conn);
  38. DataSet ds = new DataSet();
  39. mycommand.Fill(ds, "tablename");
  40. DataTable myTable = ds.Tables["tablename"];
  41. return myTable;
  42. }
  43. /// <summary>
  44. /// 能创建ACCESS表
  45. /// </summary>
  46. /// <param name="tablename"></param>
  47. /// <param name="conn"></param>
  48. /// <param name="myTable"></param>
  49. public void createtable(string tablename, string conn, DataTable myTable)
  50. {
  51. //DataTable myTable = getds(conn, Sql,tablename);
  52. OleDbConnection thisconnection1 = new OleDbConnection(connectionString);
  53. thisconnection1.Open();
  54. string strSql = "create table " + tablename + "(";
  55. foreach (System.Data.DataColumn c in myTable.Columns)
  56. {
  57. strSql += c.ColumnName + " VARCHAR,";
  58. }
  59. strSql = strSql.Substring(0, strSql.Length - 1) + ")";
  60. OleDbCommand mycommand1 = new OleDbCommand(strSql, thisconnection1);
  61. mycommand1.ExecuteNonQuery();
  62. thisconnection1.Close();
  63. }
  64. /// <summary>
  65. /// 从EXCEL文件中导入数据到ACCESS文件
  66. /// </summary>
  67. /// <param name="excelFile"></param>
  68. /// <param name="sheetName"></param>
  69. /// <param name="connectionString"></param>
  70. public void toaccess(string tablename, string conn, DataTable myTable)
  71. {
  72. //DataTable myTable = getds(conn, Sql,tablename);
  73. OleDbConnection thisconnection1 = new OleDbConnection(connectionString);
  74. thisconnection1.Open();
  75. int count = myTable.Rows.Count;
  76. for (int i = 0; i < count; i++)
  77. {
  78. string strSql1 = "insert into " + tablename + "(";
  79. foreach (System.Data.DataColumn c in myTable.Columns)
  80. {
  81. strSql1 += c.ColumnName + ",";
  82. }
  83. strSql1 = strSql1.Substring(0, strSql1.Length - 1) + ") values ('";
  84. for (int j = 0; j < myTable.Columns.Count; j++)
  85. strSql1 += myTable.Rows[i][j].ToString() + "','";
  86. strSql1 = strSql1.Substring(0, strSql1.Length - 2) + ")";
  87. OleDbCommand mycommand2 = new OleDbCommand(strSql1, thisconnection1);
  88. mycommand2.ExecuteNonQuery();
  89. }
  90. thisconnection1.Close();
  91. }
  92. /// <summary>
  93. /// 从ACCESS文件中导出数据到EXCEL文件
  94. /// </summary>
  95. /// <param name="filename">EXCEL文件名</param>
  96. /// <param name="sql">SQL语句以得到要导出的记录</param>
  97. /// <param name="connectionString">连接字符串</param>
  98. public void toexcel(string filename,string sql,string connectionString)
  99. {
  100. //StringWriter sw = new StringWriter();
  101. //OleDbConnection con = new OleDbConnection(connectionString);
  102. //OleDbDataAdapter sda = new OleDbDataAdapter();
  103. //sda.SelectCommand = new OleDbCommand(sql, con);
  104. //DataTable dt = new DataTable();
  105. //ExportExcel(dt, sw);
  106. ////attachment 参数表示作为附件下载,可以改成 online在线打开
  107. ////filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls.txt.htm
  108. ////Response.ContentType指定文件类型 可以为application/ms-excel、
  109. ////application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
  110. //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);//遠程下載
  111. //HttpContext.Current.Response.ContentType = "application/ms-excel";//或为"text/plain"
  112. //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//或为Encoding.UTF8;Response.Charset = "utf-8";
  113. //HttpContext.Current.Response.Write(sw);
  114. //HttpContext.Current.Response.End();
  115. // string conn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~\\App_Data\\xngs.mdb");
  116. OleDbConnection con = new OleDbConnection(connectionString);
  117. OleDbDataAdapter sda = new OleDbDataAdapter();
  118. sda.SelectCommand = new OleDbCommand(sql, con);
  119. DataTable dt = new DataTable();
  120. sda.Fill(dt);
  121. StringWriter sw = new StringWriter();
  122. // StreamWriter sw = new StreamWriter("d:\\test.xls",false,System.Text.Encoding.Default); //本地保存
  123. ExportExcel(dt, sw);
  124. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);//遠程下載
  125. HttpContext.Current.Response.ContentType = "application/ms-excel";
  126. HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  127. HttpContext.Current.Response.Write(sw);
  128. HttpContext.Current.Response.End();
  129. }
  130. public void ExportExcel(DataTable dt, StringWriter w)
  131. {
  132. try
  133. {
  134. for (int i = 0; i < dt.Columns.Count; i++)
  135. {
  136. w.Write(dt.Columns[i]);
  137. w.Write('\t');
  138. }
  139. w.Write("\r\n");
  140. object[] values = new object[dt.Columns.Count];
  141. foreach (DataRow dr in dt.Rows)
  142. {
  143. values = dr.ItemArray;
  144. for (int i = 0; i < dt.Columns.Count; i++)
  145. {
  146. w.Write(values[i]);
  147. w.Write('\t'); //修改语句
  148. }
  149. w.Write("\r\n"); //修改语句
  150. }
  151. // w.Flush();
  152. w.Close();
  153. }
  154. catch
  155. {
  156. w.Close();
  157. }
  158. }
  159. }