导出 excel .net asp.net
来源:互联网 发布:2016com域名注册 促销 编辑:程序博客网 时间:2024/06/18 17:48
public string ExportToExcel(System.Web.UI.Page objPage, DataView dbDtViewWrk, string[] cols)
{
if ((dbDtViewWrk.Count == 0) || (dbDtViewWrk == null))
{
return "-2";
}
if ((cols.Length == 0) || (cols == null))
{
return "-2";
}
string strAbsolutePath = clsCommon.GetUploadFilePath(objPage, clsCommon.genmUploadFileKind.Templete, "");
string strRelativePath = clsCommon.GetUploadFileUrl(objPage, clsCommon.genmUploadFileKind.Templete, "");
Object Nothing = System.Reflection.Missing.Value;
//取得Excel文件保存路径
string strFileNm = DateTime.Now.ToString("yyyyMMddHHmmssffff");
string strFileName = strFileNm + ".XLS";
object filename = strAbsolutePath + strFileName;
Excel.Application myExcel = new Excel.ApplicationClass();
try
{
//创建excel文件
myExcel.Application.Workbooks.Add(Nothing);
for (int j = 1; j <= cols.Length; j++)
{
myExcel.Cells[1, j] = cols[j - 1];
}
for (int i = 1; i <= dbDtViewWrk.Count; i++)
{
for (int j = 1; j <= cols.Length; j++)
{
myExcel.Cells[i + 1, j] = dbDtViewWrk[i - 1][j - 1];
}
}
//保存excel文件到指定的目录下
myExcel.ActiveWorkbook.SaveAs(filename, Nothing, Nothing, Nothing, Nothing, Nothing, Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭Excel Work Book对象
myExcel.ActiveWorkbook.Close(Nothing, Nothing, Nothing);
//关闭Excel组件对象
myExcel.Quit();
}
catch (Exception e)
{
return "-1";
}
finally
{
//释放相关资源
myExcel = null;
GC.Collect();
}
return strRelativePath + strFileName;
}
public string ExportToExcel(System.Web.UI.Page objPage, DataTable dbDtViewWrk)
{
if (dbDtViewWrk == null)
{
return "-2";
}
string strAbsolutePath = clsCommon.GetUploadFilePath(objPage, clsCommon.genmUploadFileKind.Templete, "");
string strRelativePath = clsCommon.GetUploadFileUrl(objPage, clsCommon.genmUploadFileKind.Templete, "");
int col=dbDtViewWrk.Columns.Count;
int row=dbDtViewWrk.Rows.Count;
Object Nothing = System.Reflection.Missing.Value;
//取得Excel文件保存路径
string strFileNm = DateTime.Now.ToString("yyyyMMddHHmmssffff");
string strFileName = strFileNm + ".XLS";
object filename = strAbsolutePath + strFileName;
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
//创建excel文件
myExcel.Application.Workbooks.Add(Nothing);
for (int j = 1; j <= col; j++)
{
myExcel.Cells[1, j] = dbDtViewWrk.Columns[j].ColumnName;
}
for (int i = 0; i < col; i++)
{
for (int j = 1; j <= row; j++)
{
myExcel.Cells[i + 1, j] = dbDtViewWrk.Rows[i][j - 1].ToString();
}
}
//保存excel文件到指定的目录下
myExcel.ActiveWorkbook.SaveAs(filename, Nothing, Nothing, Nothing, Nothing, Nothing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭Excel Work Book对象
myExcel.ActiveWorkbook.Close(Nothing, Nothing, Nothing);
//关闭Excel组件对象
myExcel.Quit();
}
catch (Exception e)
{
return "-1";
}
finally
{
//释放相关资源
myExcel = null;
GC.Collect();
}
return strRelativePath + strFileName;
}
- asp.net导出excel
- asp.net导出Excel
- asp.net导出excel
- asp.net导出Excel
- Asp.net 导出Excel
- asp.net导出excel
- asp.net 导出excel
- asp.net 导出Excel
- asp.net 导出 excel
- asp.net导出Excel
- ASP.NET Excel导出
- asp.net导出EXCEL
- ASP.NET Excel导出
- asp.net 导出Excel
- asp.net 导出Excel
- Asp.net导出Excel
- asp.net导出excel
- Asp.net 导出Excel
- 乔布斯改变世界的10种方式
- C++笔试题(不断更新)
- mysql与oracle日期格式化字符对照表
- VSTO Ribbons的完整介绍
- The Busiest Man 强连通分量+缩点+传递闭包 !
- 导出 excel .net asp.net
- 拷问工程师如何成为职场的超级巨星?
- C++数组指针和指针数组的分析
- MySql存储过程—3、变量
- 脚本过滤代码
- handler 理解(1)定义
- 屏蔽在input中输入js代码
- FZU 1894 志愿者选拔(简单单调队列)
- 43个免费的云计算技术服务