从DataView中生成Excel报表的方案(C#)
生活随笔
收集整理的這篇文章主要介紹了
从DataView中生成Excel报表的方案(C#)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
正文:
一、首先要引用一個(gè)Excel的組件,我一開始是在Office XP下嘗試的,不
成功,后來(lái)把XP給干掉,裝2k,就成功了,所以這里分享的是Office 2k下
引用相關(guān)組件來(lái)實(shí)現(xiàn)功能的,在工程中引用COM標(biāo)簽中的Microsoft
Excel 9.0 Object Library,添加成功后,引用中會(huì)多出三個(gè)引用項(xiàng):
Excel、Office、VBIDE。
二、具體代碼。
using System;
using System.Data;
using Excel;
using System.IO;
namespace Test.ExcelCom
{
/// <summary>
/// 將DataView中的數(shù)據(jù)導(dǎo)入Excel文件中
/// 作者:Rexsp
/// 創(chuàng)建:2004-4-4
/// </summary>
public class OutputExcel
{
#region 私有成員
/// <summary>
/// 數(shù)據(jù)的DataView
/// </summary>
private DataView dv=null;
/// <summary>
/// 表格標(biāo)題
/// </summary>
private string title=null;
/// <summary>
/// 輸出文件路徑
/// </summary>
private string outFilePath=null;
/// <summary>
/// 輸入文件名
/// </summary>
private string inputFilePath=null;
#endregion
#region 公共屬性
/// <summary>
/// 數(shù)據(jù)的DataView
/// </summary>
public DataView DV
{
set{dv=value;}
}
/// <summary>
/// 表格標(biāo)題
/// </summary>
public string Title
{
set{title=value;}
get{return title;}
}
/// <summary>
/// 輸出文件路徑
/// </summary>
public string OutFilePath
{
set{outFilePath=value;}
get{return outFilePath;}
}
/// <summary>
/// 輸入文件路徑
/// </summary>
public string InputFilePath
{
set{inputFilePath=value;}
get{return inputFilePath;}
}
#endregion
#region 構(gòu)造函數(shù)
public OutputExcel()
{
}
public OutputExcel(DataView dv,string title)
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
#endregion
#region 公共方法
public void CreateExcel()
{
int rowIndex=4;//行起始坐標(biāo)
int colIndex=1;//列起始坐標(biāo)
ApplicationClass myApp=null;
Workbook myBook=null;
Worksheet mySheet=null;
//如果文件不存在,則將模板文件拷貝一份作為輸出文件
//這里如果通過(guò)File.Create來(lái)創(chuàng)建文件是不行的,因?yàn)閤ls
//的空文件也有固定的格式,跟文本不一樣的,也許有其它
//通過(guò)程序直接生成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);
myBook=myApp.Workbooks[1];
mySheet=(Worksheet)myBook.ActiveSheet;
//
//取得標(biāo)題
//
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;
//設(shè)置標(biāo)題格式為居中對(duì)齊
}
//
//取得表格中的數(shù)據(jù)
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設(shè)置日期型的字段格式為居中對(duì)齊
}
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;//設(shè)置字符型的字段格式為居中對(duì)齊
}
else
{
mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加載一個(gè)合計(jì)行
//
int rowSum = rowIndex + 1;
int colSum = 2;
mySheet.Cells[rowSum,2] = "合計(jì)";
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//設(shè)置選中的部分的顏色
//
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;//設(shè)置為淺黃色,共計(jì)有56種
//
//取得整個(gè)報(bào)表的標(biāo)題
//
mySheet.Cells[2,2] = title;
//
//設(shè)置整個(gè)報(bào)表的標(biāo)題格式
//
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;
//
//設(shè)置報(bào)表表格為最適應(yīng)寬度
//
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();
//
//設(shè)置整個(gè)報(bào)表的標(biāo)題為跨列居中
//
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;//設(shè)置左邊線加粗
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//設(shè)置上邊線加粗
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//設(shè)置右邊線加粗
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//設(shè)置下邊線加粗
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();
}
#endregion
}
}
一點(diǎn)說(shuō)明:操作Excel的時(shí)候,可能會(huì)發(fā)生Excel進(jìn)程被鎖定,無(wú)法退
出,解決方法是在保存完并關(guān)閉myBook(工作簿)后,別關(guān)閉Excel進(jìn)
程(//myApp.Quit();)。這樣的結(jié)果是服務(wù)器上始終有一個(gè)Excel的
進(jìn)程。可能會(huì)出現(xiàn)asp_net用戶操作Excel的權(quán)限不夠,配置Dcom。運(yùn)
行Dcomcnfg.exe,找到Excel應(yīng)用程序,配置其屬性,身份驗(yàn)證級(jí)別
選"無(wú)",身份標(biāo)識(shí)選"交互式用戶",安全性頁(yè)面,啟動(dòng)和訪問(wèn)均給
everyone。注意:查看當(dāng)前進(jìn)程中是否有Winword進(jìn)程存在,如果有且
不能被結(jié)束,那么重啟動(dòng)計(jì)算機(jī)。再次運(yùn)行你的代碼即OK。這樣以后
就不會(huì)出現(xiàn)權(quán)限不夠的情況了。
三、調(diào)用
#region 測(cè)試Excel
QuickItemCollection qic =new QuickItemCollection();
qic.GetAllInfo();
DataView dv= new DataView();
DataTable dt = new DataTable("Excel");
dt.Columns.Add("ID",System.Type.GetType("System.String"));
dt.Columns.Add("ItemName",System.Type.GetType("System.String"));
int qicCount=qic.Count;
for(int i=0;i<qicCount;i++)
{
DataRow dr= dt.NewRow();
dr[0] = qic[i].ID;
dr[1] = qic[i].ItemName;
dt.Rows.Add(dr);
}
OutputExcel ope = new OutputExcel();
ope.DV=dt.DefaultView;
ope.Title="測(cè)試生成Excel";
ope.InputFilePath=Server.MapPath("Sample.xls");
ope.OutFilePath=Server.MapPath("Test.xls");
ope.CreateExcel();
#endregion?
?
一、首先要引用一個(gè)Excel的組件,我一開始是在Office XP下嘗試的,不
成功,后來(lái)把XP給干掉,裝2k,就成功了,所以這里分享的是Office 2k下
引用相關(guān)組件來(lái)實(shí)現(xiàn)功能的,在工程中引用COM標(biāo)簽中的Microsoft
Excel 9.0 Object Library,添加成功后,引用中會(huì)多出三個(gè)引用項(xiàng):
Excel、Office、VBIDE。
二、具體代碼。
using System;
using System.Data;
using Excel;
using System.IO;
namespace Test.ExcelCom
{
/// <summary>
/// 將DataView中的數(shù)據(jù)導(dǎo)入Excel文件中
/// 作者:Rexsp
/// 創(chuàng)建:2004-4-4
/// </summary>
public class OutputExcel
{
#region 私有成員
/// <summary>
/// 數(shù)據(jù)的DataView
/// </summary>
private DataView dv=null;
/// <summary>
/// 表格標(biāo)題
/// </summary>
private string title=null;
/// <summary>
/// 輸出文件路徑
/// </summary>
private string outFilePath=null;
/// <summary>
/// 輸入文件名
/// </summary>
private string inputFilePath=null;
#endregion
#region 公共屬性
/// <summary>
/// 數(shù)據(jù)的DataView
/// </summary>
public DataView DV
{
set{dv=value;}
}
/// <summary>
/// 表格標(biāo)題
/// </summary>
public string Title
{
set{title=value;}
get{return title;}
}
/// <summary>
/// 輸出文件路徑
/// </summary>
public string OutFilePath
{
set{outFilePath=value;}
get{return outFilePath;}
}
/// <summary>
/// 輸入文件路徑
/// </summary>
public string InputFilePath
{
set{inputFilePath=value;}
get{return inputFilePath;}
}
#endregion
#region 構(gòu)造函數(shù)
public OutputExcel()
{
}
public OutputExcel(DataView dv,string title)
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
#endregion
#region 公共方法
public void CreateExcel()
{
int rowIndex=4;//行起始坐標(biāo)
int colIndex=1;//列起始坐標(biāo)
ApplicationClass myApp=null;
Workbook myBook=null;
Worksheet mySheet=null;
//如果文件不存在,則將模板文件拷貝一份作為輸出文件
//這里如果通過(guò)File.Create來(lái)創(chuàng)建文件是不行的,因?yàn)閤ls
//的空文件也有固定的格式,跟文本不一樣的,也許有其它
//通過(guò)程序直接生成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);
myBook=myApp.Workbooks[1];
mySheet=(Worksheet)myBook.ActiveSheet;
//
//取得標(biāo)題
//
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;
//設(shè)置標(biāo)題格式為居中對(duì)齊
}
//
//取得表格中的數(shù)據(jù)
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設(shè)置日期型的字段格式為居中對(duì)齊
}
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;//設(shè)置字符型的字段格式為居中對(duì)齊
}
else
{
mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加載一個(gè)合計(jì)行
//
int rowSum = rowIndex + 1;
int colSum = 2;
mySheet.Cells[rowSum,2] = "合計(jì)";
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//設(shè)置選中的部分的顏色
//
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;//設(shè)置為淺黃色,共計(jì)有56種
//
//取得整個(gè)報(bào)表的標(biāo)題
//
mySheet.Cells[2,2] = title;
//
//設(shè)置整個(gè)報(bào)表的標(biāo)題格式
//
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;
//
//設(shè)置報(bào)表表格為最適應(yīng)寬度
//
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();
//
//設(shè)置整個(gè)報(bào)表的標(biāo)題為跨列居中
//
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;//設(shè)置左邊線加粗
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//設(shè)置上邊線加粗
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//設(shè)置右邊線加粗
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//設(shè)置下邊線加粗
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();
}
#endregion
}
}
一點(diǎn)說(shuō)明:操作Excel的時(shí)候,可能會(huì)發(fā)生Excel進(jìn)程被鎖定,無(wú)法退
出,解決方法是在保存完并關(guān)閉myBook(工作簿)后,別關(guān)閉Excel進(jìn)
程(//myApp.Quit();)。這樣的結(jié)果是服務(wù)器上始終有一個(gè)Excel的
進(jìn)程。可能會(huì)出現(xiàn)asp_net用戶操作Excel的權(quán)限不夠,配置Dcom。運(yùn)
行Dcomcnfg.exe,找到Excel應(yīng)用程序,配置其屬性,身份驗(yàn)證級(jí)別
選"無(wú)",身份標(biāo)識(shí)選"交互式用戶",安全性頁(yè)面,啟動(dòng)和訪問(wèn)均給
everyone。注意:查看當(dāng)前進(jìn)程中是否有Winword進(jìn)程存在,如果有且
不能被結(jié)束,那么重啟動(dòng)計(jì)算機(jī)。再次運(yùn)行你的代碼即OK。這樣以后
就不會(huì)出現(xiàn)權(quán)限不夠的情況了。
三、調(diào)用
#region 測(cè)試Excel
QuickItemCollection qic =new QuickItemCollection();
qic.GetAllInfo();
DataView dv= new DataView();
DataTable dt = new DataTable("Excel");
dt.Columns.Add("ID",System.Type.GetType("System.String"));
dt.Columns.Add("ItemName",System.Type.GetType("System.String"));
int qicCount=qic.Count;
for(int i=0;i<qicCount;i++)
{
DataRow dr= dt.NewRow();
dr[0] = qic[i].ID;
dr[1] = qic[i].ItemName;
dt.Rows.Add(dr);
}
OutputExcel ope = new OutputExcel();
ope.DV=dt.DefaultView;
ope.Title="測(cè)試生成Excel";
ope.InputFilePath=Server.MapPath("Sample.xls");
ope.OutFilePath=Server.MapPath("Test.xls");
ope.CreateExcel();
#endregion?
?
總結(jié)
以上是生活随笔為你收集整理的从DataView中生成Excel报表的方案(C#)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一般纳税人代理记账多少钱一个月?
- 下一篇: C# 线程无法开启窗口的原因