导出excel的另外一种方法
据我现在所知excel有四种方法:
1.自己写的excel接口,客户端不需要装excel,见灵感之源的blog:
http://www.cnblogs.com/unruledboy/archive/2004/07/07/22093.aspx
2.把web上的DataGrid直接导入到excel
3.在引用里调用Microsoft.Office.Interop.Excel.dll,原理是把数据存到DataTable、DataView或DataGrid中,然后再把数据一格一格的赋到excel的cell里去。
见如下代码:
4.另外,这就是另外一种方法了,建一个SqlServer的数据源,利用Excel的外部数据源让Excel自己从数据库取数据:
这里的pstrSql指的是sql语句。
1.自己写的excel接口,客户端不需要装excel,见灵感之源的blog:
http://www.cnblogs.com/unruledboy/archive/2004/07/07/22093.aspx
2.把web上的DataGrid直接导入到excel
public void ExportToExcel(System.Web.UI.Control ctl)
{
bool CurrCtlVisible=ctl.Visible;
ctl.Visible=true; Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
ctl.Page.EnableViewState = true;
ctl.Visible=CurrCtlVisible;
}
{
bool CurrCtlVisible=ctl.Visible;
ctl.Visible=true; Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
ctl.Page.EnableViewState = true;
ctl.Visible=CurrCtlVisible;
}
3.在引用里调用Microsoft.Office.Interop.Excel.dll,原理是把数据存到DataTable、DataView或DataGrid中,然后再把数据一格一格的赋到excel的cell里去。
见如下代码:
public class ExportToExcel
{
私有成员
公共属性
构造函数
公共方法
}
{
私有成员
公共属性
构造函数
公共方法
}
4.另外,这就是另外一种方法了,建一个SqlServer的数据源,利用Excel的外部数据源让Excel自己从数据库取数据:
public void ExportToExcel(string pstrSql)
{
Excel.Application pApplication;
Excel._Workbook xBk;
Excel._Worksheet xSt;
Excel._QueryTable xQt;
string ExcelConn = "ODBC;DRIVER=SQL Server;SERVER=localhost;UID=sa;PWD=;APP=Microsoft(R) Windows (R) 2000 Operating System;WSID=me;DATABASE=pubs";
pApplication = new Excel.ApplicationClass();
xBk = pApplication.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.ActiveSheet;
pApplication.Cells[2,2] = this.title;
xSt.get_Range(pApplication.Cells[2,2],pApplication.Cells[2,2]).Font.Bold = true;
xSt.get_Range(pApplication.Cells[2,2],pApplication.Cells[2,2]).Font.Name = "黑体";
xSt.get_Range(pApplication.Cells[2,2],pApplication.Cells[2,2]).Font.Size = 22;
xQt = xSt.QueryTables.Add(ExcelConn,xSt.get_Range(pApplication.Cells[4,2],pApplication.Cells[4,2]),pstrSql);
xQt.Name = "导出EXCEL";
xQt.FieldNames = true;
xQt.RowNumbers = false;
xQt.FillAdjacentFormulas = false;
xQt.PreserveFormatting = false;
xQt.BackgroundQuery = true;
xQt.RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells;
xQt.AdjustColumnWidth = true;
xQt.RefreshPeriod = 0;
xQt.PreserveColumnInfo = true;
xQt.Refresh(xQt.BackgroundQuery);
pApplication.Visible = true;
}
{
Excel.Application pApplication;
Excel._Workbook xBk;
Excel._Worksheet xSt;
Excel._QueryTable xQt;
string ExcelConn = "ODBC;DRIVER=SQL Server;SERVER=localhost;UID=sa;PWD=;APP=Microsoft(R) Windows (R) 2000 Operating System;WSID=me;DATABASE=pubs";
pApplication = new Excel.ApplicationClass();
xBk = pApplication.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.ActiveSheet;
pApplication.Cells[2,2] = this.title;
xSt.get_Range(pApplication.Cells[2,2],pApplication.Cells[2,2]).Font.Bold = true;
xSt.get_Range(pApplication.Cells[2,2],pApplication.Cells[2,2]).Font.Name = "黑体";
xSt.get_Range(pApplication.Cells[2,2],pApplication.Cells[2,2]).Font.Size = 22;
xQt = xSt.QueryTables.Add(ExcelConn,xSt.get_Range(pApplication.Cells[4,2],pApplication.Cells[4,2]),pstrSql);
xQt.Name = "导出EXCEL";
xQt.FieldNames = true;
xQt.RowNumbers = false;
xQt.FillAdjacentFormulas = false;
xQt.PreserveFormatting = false;
xQt.BackgroundQuery = true;
xQt.RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells;
xQt.AdjustColumnWidth = true;
xQt.RefreshPeriod = 0;
xQt.PreserveColumnInfo = true;
xQt.Refresh(xQt.BackgroundQuery);
pApplication.Visible = true;
}
这里的pstrSql指的是sql语句。