
如何将数据库中的数据导出到excel
3个回答
2015-04-27 · 知道合伙人数码行家

知道合伙人数码行家
采纳数:117533
获赞数:517216
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注

展开全部
以下都只是介绍操作的原理,具体要求要在应用中具体分析改变。
1. 此方法常用在form或者Console Application中,使用时须用要添加Reference,具体做法:
右键点击项目添加“Add Reference”,在Tom组件下,选择“Microsoft Excel 14.0 Object Library”,然后在项目中使用
下面注释//it looks like excele table start with 1 not 1
应该为//it looks like excele table start with 1 not 0
[csharp] view plaincopy
private static void exportToExcel(DataTable dt)
{
Excel.Application excel=new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = true;
//get the columns
for (int i = 0; i < dt.Columns.Count;i++ )
{
//here is started with 1
//it looks like excele table start with 1 not 1
excel.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
}
//get the data in rows
for (int row = 0; row < dt.Rows.Count;row++ )
{
for (int col = 0; col < dt.Columns.Count; col++)
{
excel.Cells[row+2, col+1] = dt.Rows[row][dt.Columns[col]].ToString();
}
}
//FolderBrowserDialog path = new FolderBrowserDialog();//打开文件对话框
//path.ShowDialog();
//textBox1.Text = path.SelectedPath;//选择文件夹
//save excel
//excel.SaveWorkspace();
excel.Quit();
}
2. 在web应用中,可通过HttpContext.Response.write()来实现
[csharp] view plaincopy
protected static void toExcel(DataTable da){
System.Web.HttpContext context = System.Web.HttpContext.Current;
context.Response.Clear();
foreach( DataColumn colum in da.Columns){
context.Response.Write(colum.ColumnName+"\t");
}
context.Response.Write(System.Environment.NewLine);
foreach (DataRow row in da.Rows) {
for (int i = 0; i < da.Rows.Count; i++)
{
context.Response.Write(row[i].ToString()+"\t");
}
context.Response.Write(System.Environment.NewLine);
}
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=plan.xls");
context.Response.End();
}
1. 此方法常用在form或者Console Application中,使用时须用要添加Reference,具体做法:
右键点击项目添加“Add Reference”,在Tom组件下,选择“Microsoft Excel 14.0 Object Library”,然后在项目中使用
下面注释//it looks like excele table start with 1 not 1
应该为//it looks like excele table start with 1 not 0
[csharp] view plaincopy
private static void exportToExcel(DataTable dt)
{
Excel.Application excel=new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = true;
//get the columns
for (int i = 0; i < dt.Columns.Count;i++ )
{
//here is started with 1
//it looks like excele table start with 1 not 1
excel.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
}
//get the data in rows
for (int row = 0; row < dt.Rows.Count;row++ )
{
for (int col = 0; col < dt.Columns.Count; col++)
{
excel.Cells[row+2, col+1] = dt.Rows[row][dt.Columns[col]].ToString();
}
}
//FolderBrowserDialog path = new FolderBrowserDialog();//打开文件对话框
//path.ShowDialog();
//textBox1.Text = path.SelectedPath;//选择文件夹
//save excel
//excel.SaveWorkspace();
excel.Quit();
}
2. 在web应用中,可通过HttpContext.Response.write()来实现
[csharp] view plaincopy
protected static void toExcel(DataTable da){
System.Web.HttpContext context = System.Web.HttpContext.Current;
context.Response.Clear();
foreach( DataColumn colum in da.Columns){
context.Response.Write(colum.ColumnName+"\t");
}
context.Response.Write(System.Environment.NewLine);
foreach (DataRow row in da.Rows) {
for (int i = 0; i < da.Rows.Count; i++)
{
context.Response.Write(row[i].ToString()+"\t");
}
context.Response.Write(System.Environment.NewLine);
}
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=plan.xls");
context.Response.End();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询