将dataview导出到excel中

GFHFGHFG

 using System;
using System.Data;
using Excel;
using System.IO;
namespace CNC_PMIS.Webs.ExcelCom
{
/// <summary>
/// OutputExcel 的摘要说明。
/// </summary>
public class OutputExcel
{

private DataView dv=null;

private string title=null;

private string outFilePath=null;

private string inputFilePath=null;

public DataView DV
{
set{dv=value;}
}
public string Title
{
set{title=value;}
get{return title;}
}

public string OutFilePath
{
set{outFilePath=value;}
get{return outFilePath;}
}

public string InputFilePath
{
set{inputFilePath=value;}
get{return inputFilePath;}
}

public OutputExcel()
{
}

/*
public OutputExcel(DataView dv,string title)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
*/
public void CreateExcel()
{
int rowIndex=4;//行起始坐标
int colIndex=1;//列起始坐标
ApplicationClass myApp=null;
Workbook myBook=null;
Worksheet mySheet=null;
//如果文件不存在,则将模板文件拷贝一份作为输出文件
//这里如果通过File.Create来创建文件是不行的,因为xls
//的空文件也有固定的格式,跟文本不一样的,也许有其它
//通过程序直接生成excel的方法,大家可以尝试尝试的
if(!File.Exists(outFilePath))
{
File.Copy(inputFilePath,outFilePath,true);
}

myApp= new ApplicationClass();
myApp.Visible=false;
object oMissiong=System.Reflection.Missing.Value;
myApp.Workbooks.Open(outFilePath,oMissiong,oMissiong,oMissiong,oMissiong,
oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
myBook=myApp.Workbooks[1];
mySheet=(Worksheet)myBook.ActiveSheet;

//
//取得标题
//
foreach(DataColumn col in dv.Table.Columns)
{
colIndex++;
mySheet.Cells[4,colIndex] = col.ColumnName;
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
//设置标题格式为居中对齐
}
//
//取得表格中的数据
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
try
{
mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
}
catch
{}
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment =

XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else if(col.DataType == System.Type.GetType("System.String"))
{
mySheet.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment =

XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}

//
//加载一个合计行
//
//int rowSum = rowIndex + 1; //注释掉,不显示合计行
int rowSum = rowIndex;
//int colSum = 2; //注释掉,合计行的列数
// mySheet.Cells[rowSum,2] = "合计"; //注释掉,不显示合计行
// mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter; //注

释掉,不显示合计行
//
//设置选中的部分的颜色
//
// mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Select(); //注释掉,不显示合计行
// mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色

,共计有56种 //注释掉,不显示合计行
//
//取得整个报表的标题
//
mySheet.Cells[2,2] = title;
//
//设置整个报表的标题格式
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Bold = true;
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).HorizontalAlignment =

XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight =

XlBorderWeight.xlThick;//设置左边线加粗
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight =

XlBorderWeight.xlThick;//设置上边线加粗
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight =

XlBorderWeight.xlThick;//设置右边线加粗
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight =

XlBorderWeight.xlThick;//设置下边线加粗
myBook.Save();;
myBook.Close( true,outFilePath,true);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);
GC.Collect();

}

public void CreateExcelNoTitle()
{
int rowIndex=3;//行起始坐标
int colIndex=1;//列起始坐标
ApplicationClass myApp=null;
Workbook myBook=null;
Worksheet mySheet=null;
//如果文件不存在,则将模板文件拷贝一份作为输出文件
//这里如果通过File.Create来创建文件是不行的,因为xls
//的空文件也有固定的格式,跟文本不一样的,也许有其它
//通过程序直接生成excel的方法,大家可以尝试尝试的
if(!File.Exists(outFilePath))
{
File.Copy(inputFilePath,outFilePath,true);
}

myApp= new ApplicationClass();
myApp.Visible=false;
object oMissiong=System.Reflection.Missing.Value;
myApp.Workbooks.Open(outFilePath,oMissiong,oMissiong,oMissiong,oMissiong,
oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
myBook=myApp.Workbooks[1];
mySheet=(Worksheet)myBook.ActiveSheet;

//
//取得标题
//
// foreach(DataColumn col in dv.Table.Columns)
// {
// colIndex++;
// mySheet.Cells[4,colIndex] = col.ColumnName;
// mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
// //设置标题格式为居中对齐
// }
//
//取得表格中的数据
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{

colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
try
{
mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
}
catch
{}
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment =

XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else if(col.DataType == System.Type.GetType("System.String"))
{
mySheet.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment =

XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}

}

//
//加载一个合计行
//
//int rowSum = rowIndex + 1; //注释掉,不显示合计行
int rowSum = rowIndex;
//int colSum = 2; //注释掉,合计行的列数
// mySheet.Cells[rowSum,2] = "合计"; //注释掉,不显示合计行
// mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;

//注释掉,不显示合计行
//
//设置选中的部分的颜色
//
// mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Select(); //注释掉,不显示合计行
// mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为

浅黄色,共计有56种 //注释掉,不显示合计行
//
//取得整个报表的标题
//
mySheet.Cells[2,2] = title;
//
//设置整个报表的标题格式
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Bold = true;
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).HorizontalAlignment =

XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight =

XlBorderWeight.xlThick;//设置左边线加粗
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight =

XlBorderWeight.xlThick;//设置上边线加粗
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight =

XlBorderWeight.xlThick;//设置右边线加粗
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight =

XlBorderWeight.xlThick;//设置下边线加粗
myBook.Save();;
myBook.Close( true,outFilePath,true);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);
GC.Collect();

}
}
}


调用方法

OutputExcel ope = new OutputExcel();
//ope.DV=dt.DefaultView;
ope.DV=dTable.DefaultView;

ope.Title="收入合同列表";
string strFileName="srhtlb.xls";
ope.InputFilePath=Server.MapPath("Sample.xls");
if(System.IO.File.Exists(Server.MapPath(strFileName)))
{
System.IO.File.Delete(Server.MapPath(strFileName));
}
ope.OutFilePath=Server.MapPath(strFileName);
ope.CreateExcel();

//下载
string path = Server.MapPath(strFileName);

System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行

Response.End();

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值