关于导出excel 把一个dataset的多个datatable导入到一个excel的多个sheet中
来源:互联网 发布:喷绘王软件 编辑:程序博客网 时间:2024/05/16 18:51
几经查询资料,我完成了这个看似结果还不错的任务,在解决任务的过程也也遇到了很多的问题,但最后胜利的成果还是要和大家分享一下,
大家看看有没有什么问题,多提提意见,有更好的可以给我看看,我需要。
代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HCK.BLL;
using HCK.Model;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
中间省略
/// <summary)
/// 导出excel
/// </summary>
/// <param name="ds"></param>
/// <param name="tableNames"></param>
/// <param name="FileName"></param>
public void doExport(DataSet ds, string[] tableNames, string FileName)
{
string path = "";
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
try
{
excel.Visible = false;
//设置禁止弹出保存和覆盖的询问提示框
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
//增加一个工作簿
Workbook book = excel.Workbooks.Add(true);
//添加工作表
Worksheet sheets = (Microsoft.Office.Interop.Excel.Worksheet)
book.Worksheets.Add(Missing.Value, Missing.Value, 9, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
for (int i = 0; i <ds.Tables.Count; i++)
{
System.Data.DataTable table = ds.Tables[i];
//获取一个工作表
Worksheet sheet = book.Worksheets[i + 1] as Worksheet;
int rowIndex = 1;
int colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
sheet.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
//sheet.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
//在这里要在数字前加前单引号
String typeName = row[col.ColumnName].GetType().ToString();
sheet.Cells[rowIndex, colIndex] = typeCheckAdd(row[col.ColumnName].ToString(), typeName);
}
}
sheet.Name = tableNames[i];
}
path = Server.MapPath("../") + @"excel/" + FileName + ".xls";
Page.ClientScript.RegisterStartupScript(this.GetType(), "abc", "alert('" + path + "')", true);
//book.Save();
book.SaveAs(path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//excel.Save(strExcelFileName);
}
catch (Exception err)
{
string mess = err.Message.ToString();
Page.ClientScript.RegisterStartupScript(this.GetType(), "abc", "alert('" + mess + "')", true);
}
finally
{
excel.Quit();
//excel = null;
IntPtr t = new IntPtr(excel.Hwnd); //得到这个句柄,具体作用是得到这块内存入口
int k = 0;
GetWindowThreadProcessId(t, out k); //得到本进程唯一标志k
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到对进程k的引用
p.Kill();
GC.Collect();
}
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/ms-excel";
Response.WriteFile(file.FullName);
Response.End();
//System.IO.FileInfo deletefile = new FileInfo(path);//删除生成的文件
//if (deletefile.Exists)
//{
// deletefile.Delete();
//}
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
//若是大数需加前导引号变成字符串
public String typeCheckAdd(String cellContent, String strType)
{
String cellContentAdd;
switch (strType)
{
case "System.String":
cellContentAdd = "'" + cellContent;
break;
default:
cellContentAdd = cellContent;
break;
}
return cellContentAdd;
}
思路大概就是先把生成的excel生成到项目中,然后下载到客户端,本想着把项目中的excel删除,没有成功,想着反正也没有很多,一直都是一个,也就没有管它了。 对与那个关于excel.exe进程也查了一些资料,成功了,不知道是否理想。大家多多提提意见。
- 关于导出excel 把一个dataset的多个datatable导入到一个excel的多个sheet中
- 导出Excel 把一个dataset的多个datatable导入到一个excel的多个sheet中
- 导出Excel 把一个dataset的多个datatable导入到一个excel的多个sheet中
- 将Dataset中的多个DataTable导入到一个Excel文件的多个Sheet中
- 导出DataSet数据到excel的多个sheet
- 把DataSet的数据导入到Excel的多个sheet
- ASP.NET将数据导出到一个EXCEL文件的多个SHEET中
- C# 如何将 不同的数据(多个查询结果集 dataset)导出到一个excel的不同sheet中
- 将Excel中所有Sheet的数据导入一个DataSet
- 将Excel中所有Sheet的数据导入一个DataSet
- 如何将dataset导入到excel表中的多个sheet中
- POI导出数据到Excel的多个Sheet
- 把多个excel文件的sheet1数据合并到一个excel文件的sheet1中
- 用VB把多个excel文件的数据顺序拷到一个excel中
- 把DATASET里的多个表导出到一个excel文件?
- 多个Excel文件中的多个Sheet合并到一个Excel文件中两个函数
- 用VBA实现把多个Excel文件合并到一个Excel文件的多个工作表(Sheet)里
- 导出数据到excel多个sheet
- 平台崩溃之operator new异常(九)-2010-5-20
- Tips: linux shell重定向
- Color Me Less--poj--1046
- Qt之对话框
- Flash Games
- 关于导出excel 把一个dataset的多个datatable导入到一个excel的多个sheet中
- java.nio 包的学习
- PHP网站开发方案(开发新人必读)
- 储存域网络SAN导论与基本结构
- vc++之CButton
- MDL
- 本店有大量优质手机,质量有保证,服务周到
- vc++之getwindowtext
- PHP网站开发方案(开发新人必读)