asp.net的com方式导出excel
最近做个项目,需要导出excel,要主从表一起导出的,就大致写了一个方法,可能格式方面还是会进行修改,先贴出来方便以后查看
项目引用微软excel的com的dll即可
/// <summary>
/// 功能:导出文件(通过数据源导出,可主从表导出)
/// </summary>
/// <param name="strTitle">文件名字</param>
/// <param name="mainDT">主表数据源</param>
/// <param name="dtParams">从表数据源集合</param>
public static void Export(string strTitle, System.Data.DataTable mainDT, params System.Data.DataTable[] dtParams)
{
GC.Collect();
Application oApp = new Application();
Workbooks oBooks = oApp.Workbooks;
Workbook oBook = oBooks.Add(true);
Worksheet oSheet = (Worksheet)oBook.ActiveSheet;
int _rowIndex = 4;//行索引
int _cellIndex = 1;//列索引
int _maxCellIndex = 1;//最大列索引
int _mainRowIndex = 4;//主表行索引
//绘制标题
oSheet.Cells[2, 2] = strTitle;
//绘制主表数据
foreach (DataColumn col in mainDT.Columns)
{
_cellIndex++;
//设置一行两列
if (_cellIndex > 17)
{
_rowIndex++;
_cellIndex = 2;
}
oSheet.Cells[_rowIndex, _cellIndex] = col.ColumnName+":";
oSheet.get_Range(oSheet.Cells[_rowIndex, _cellIndex], oSheet.Cells[_rowIndex, _cellIndex + 2]).Select();
oSheet.get_Range(oSheet.Cells[_rowIndex, _cellIndex], oSheet.Cells[_rowIndex, _cellIndex + 2]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
_cellIndex = _cellIndex + 3;
if (col.DataType == System.Type.GetType("System.DateTime"))// 日期格式
{
oSheet.Cells[_rowIndex, _cellIndex] =Convert.ToDateTime(mainDT.R
相关文档:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb; //导入命名空间
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Data;
usi ......
在a.aspx跳转到b.aspx
通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
如果是通过通过Server.Transfer()在a.aspx跳转到b.aspx的,则在b.aspx页面,可以查找到保存在a.aspx页面中的 控件中的值,如果是Response.Redirect(),则得不到到a.aspx页面中控件的值。
如果是通过Server.Transfer("b.aspx" ......
写了一个创建虚拟目录的 WebService 程序,在测试运行时可以成功创建,但其它程序调用时,提示没有权限创建。
查了一些资料,在web.config里面的<system.web>后加上配置:
<identity impersonate="true" userName="操作系统用户名" password="用户对应的密码" />
问题解决。特此记录,权 ......