有关c#导入导出excel的问题
来源:百度知道 编辑:UC知道 时间:2024/06/09 05:29:00
我说的不打开是不显示的打开.
补充下我是用windowsapplication做的,如过有什么新方法请写的稍微详细点.
给你一个示例吧:
在下面这个类中,有两个参数传进来,一个是它的数据源,另一个是整个报表的标题字符串,具体看代码解释。
using System;
using System.Data;
using Excel;
namespace LogicLayer
{
///
/// OutputExcel 的摘要说明
///
public class OutputExcel
{
public OutputExcel(DataView dv,string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
Excel.Application excel;
int rowIndex=4;
int colIndex=1;
Excel._Workbook xBk;
Excel._Worksheet xSt;
excel= new Excel.ApplicationClass();;
xBk = excel.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.ActiveSheet;
//
//取得标题
//
foreach(DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
}
/