导出Excel的几种方式
2006-07-25 11:50
459 查看
1.DataGrid直接导出
private void Button1_Click(object sender, System.EventArgs e){
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("id",typeof(string)));
dt.Columns.Add(new DataColumn("name",typeof(string)));
dt.Columns.Add(new DataColumn("sex",typeof(string)));
DataRow dr = null;
for(int i=0;i<1000;i++)
{
dr = dt.NewRow();
dr["id"] =i.ToString();
dr["name"] = "ssss";
dr["sex"] = "男";
dt.Rows.Add(dr);
}
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
HttpContext.Current.Response.Charset ="UTF-8";
Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=a.xls" );
DataGrid1.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
2.DataTable导出
一.使用GC.Collect();释放对象
public string DataToExcel(DataTable dt,string path,string filename)
{
Excel.Application excel = null;
Excel.Workbook book = null;
Excel.Worksheet sheet = null;
try
{
GC.Collect();
excel = new Excel.ApplicationClass();
book = excel.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
sheet = (Excel.Worksheet)book.ActiveSheet;
int rowIndex=1;
int colIndex=1;
//表头
foreach(DataColumn col in dt.Columns)
{
excel.Cells[1,colIndex] = col.ColumnName.ToString();
//处理科学记数和计算问题
if(col.DataType == System.Type.GetType("System.Int16") || col.DataType == System.Type.GetType("System.int32") || col.DataType == System.Type.GetType("System.int64") || col.DataType == System.Type.GetType("System.Decimal"))
{
sheet.get_Range(excel.Cells[1,colIndex],excel.Cells[dt.Rows.Count+1,colIndex]).NumberFormatLocal = "G/通用格式";
}
else
{
sheet.get_Range(excel.Cells[1,colIndex],excel.Cells[dt.Rows.Count+1,colIndex]).NumberFormatLocal = "@";
}
colIndex++;
}
foreach(DataRow row in dt.Rows)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dt.Columns)
{
if(col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
else
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
colIndex ++;
}
}
excel.DisplayAlerts = false;
System.Random rnd = new Random();
string fileName = filename + rnd.Next(100000000).ToString() + ".xls";
this._path = path + @"Report/"+fileName;
book.SaveCopyAs(this._path);
return this._path;
}
catch(Exception ex)
{
throw ex;
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
sheet = null;
book = null;
excel = null;
GC.Collect();
}
}
二.不使用GC.Collect();释放对象
相关文章推荐
- 关于导出数据为Excel的几种方式
- java web导出excel的几种方式
- .net导出Excel几种方式比较
- java web导出excel的几种方式
- C#中Excel的导入和导出的几种基本方式
- 导出数据为Excel的几种方式
- ASP.net导出Excel的几种方式
- Jxls导出excel的若干方式总结(十五)-- 简单分析与总结
- 导出数据到Excel的几种方法
- Hive几种导出数据方式
- 导出数据到Excel的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法
- datagrid数据导出到excel文件给客户端下载的几种方法(转)
- datagrid数据导出到excel文件给客户端下载的几种方法
- python 读取命令参数、导出词向量的几种方式
- 将DataTable导出Excel的三种方式
- 导出excel的三种方式
- mysql 导出select结果到文本的几种方式
- 在.NET应用程序中访问Excel的几种方式
- Oracle中快速导入导出数据库的几种方式