using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using Excel = Microsoft.Office.Interop.Excel; namespace ExcelOutputInput { class Program { static void Main(string[] args) { string xlspath = @"c:\学生信息.xls"; string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data Source=" + xlspath; string sql = "select * from [Sheet1$]"; DataSet ds = new DataSet(); OleDbConnection conn = new OleDbConnection(connstr); conn.Open(); OleDbCommand cmd = new OleDbCommand(sql, conn); OleDbDataAdapter oda = new OleDbDataAdapter(cmd); oda.Fill(ds); conn.Close(); foreach (DataRow item in ds.Tables[0].Rows) { Console.WriteLine(item[0].ToString() + " " + item[1].ToString() + " " + item[2] + " " + item[3] + " " + item[4].ToString() + " " + item[5]); } Console.WriteLine("请按任意键导出数据到Excel表中……"); Console.ReadLine(); DoExcelExport(ds, "xxxxx"); } /// <summary> /// 将数据导出到Excel /// </summary> /// <param name="ds">数据集</param> /// <param name="strExcelFileName">导出的文件名</param> public static void DoExcelExport(DataSet ds, string strExcelFileName) { Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Application.Workbooks.Add(true); DataTable dt = ds.Tables[0]; int rowIndex = 1; int colIndex = 0; foreach (DataColumn col in dt.Columns) { colIndex++; excel.Cells[1, colIndex] = col.ColumnName; } foreach (DataRow row in dt.Rows) { rowIndex++; colIndex = 0; foreach (DataColumn col in dt.Columns) { colIndex++; excel.Cells[rowIndex, colIndex] = row[col.ColumnName]; } } excel.Visible = true; excel = null; GC.Collect(); } } } //Excel表中的第一行必须自己定义的列名
/// <summary> /// 将DataTable导出为Excel /// </summary> /// <param name="table">DataTable数据源</param> /// <param name="name">文件名</param> public static void ExportToSpreadsheet(DataTable table, string name) { Random r = new Random(); string rf = ""; for (int j = 0; j < 10; j++) { rf = r.Next(int.MaxValue).ToString(); } HttpContext context = HttpContext.Current; context.Response.Clear(); context.Response.ContentType = "text/csv"; context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + rf + ".xls"); context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble()); foreach (DataColumn column in table.Columns) { context.Response.Write(column.ColumnName + ","); //context.Response.Write(column.ColumnName + "(" + column.DataType + "),"); } context.Response.Write(Environment.NewLine); double test; foreach (DataRow row in table.Rows) { for (int i = 0; i < table.Columns.Count; i++) { switch (table.Columns[i].DataType.ToString()) { case "System.String": if (double.TryParse(row[i].ToString(), out test)) context.Response.Write("="); context.Response.Write("\"" + row[i].ToString().Replace("\"", "\"\"") + "\","); break; case "System.DateTime": if (row[i].ToString() != "") context.Response.Write("\"" + ((DateTime)row[i]).ToString("yyyy-MM-dd hh:mm:ss") + "\","); else context.Response.Write("\"" + row[i].ToString().Replace("\"", "\"\"") + "\","); break; default: context.Response.Write("\"" + row[i].ToString().Replace("\"", "\"\"") + "\","); break; } } context.Response.Write(Environment.NewLine); } context.Response.End(); }
/// <summary> /// dtData是要导出为Excel的DataTable,FileName是要导出的Excel文件名(不加.xls) /// </summary> /// <param name="dtData"></param> /// <param name="FileName"></param> private void DataTable3Excel(System.Data.DataTable dtData, String FileName) { System.Web.UI.WebControls.GridView dgExport = null; //当前对话 System.Web.HttpContext curContext = System.Web.HttpContext.Current; //IO用于导出并返回excel文件 System.IO.StringWriter strWriter = null; System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null) { //设置编码和附件格式 //System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码 curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"); curContext.Response.ContentType = "application nd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.UTF8; curContext.Response.Charset = "GB2312"; //导出Excel文件 strWriter = new System.IO.StringWriter(); htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); //为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView dgExport = new System.Web.UI.WebControls.GridView(); dgExport.DataSource = dtData.DefaultView; dgExport.AllowPaging = false; dgExport.DataBind(); //下载到客户端 dgExport.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); } }