所有文章均来自网络(除测试总结部分),如果涉及到版权问题请与我联系,我会及时删除~~~~

C#调用操作Excel的一个类(转贴)

上一篇 / 下一篇  2007-01-19 08:34:38 / 天气: 冷 / 心情: 平静

 

在这儿本来想写长一点的文章,但因为时间的关系,没有写成。现把自己做的一个小东西,C#调用Excel作报表的源代码放在这儿给大家看看。关于代码的构成,在源代码中已经有完整的代码注释了,这儿就不说什么了。

5D/LG&o DZ&j,g)t0

下面的这个类中,主要完成的功能是从数据库中逐字段读出数据,设置格式后,在Excel中显示出来。这是它运行后的效果图:

9?4K~fVMnaEp0

c hmG xo[(_5Eq&S0

在这个类中,有两个参数传进来,一个是它的数据源,另一个是整个报表的标题字符串,具体看代码就应该知道了。51Testing软件测试网8a R-o3y F weFOO

using System;

(s.R&lmo/q.f0

using System.Data;

1lye M(^Cd0

using Excel;51Testing软件测试网 kK._k-J;X%_@['A!Aq

namespace LogicLayer51Testing软件测试网*\rZF7y y)q0JG

{51Testing软件测试网:\ks[Y0E@

 /// <summary>

1g0R&U nQi0

 /// OutputExcel 的摘要说明

6oQs7bv wv0

 /// </summary>

R(\xXY6K/ER0

 public class OutputExcel51Testing软件测试网T }4g4js;s

 {

| q;]OgQ$rd2`5]0

  public OutputExcel(DataView dv,string str)51Testing软件测试网zn6MB2n)?:v

  {

`%@_R Z3AM$]3_;r0

   //

t vWKF-q$\;I%T4L0

   // TODO: 在此处添加构造函数逻辑

6Mf7nL&~WWtx0

   //51Testing软件测试网!Y.B#EaU

   Excel.Application excel;

Q-@M$j*moE0

   int rowIndex=4;

"{^e%oa0

   int colIndex=1;

v;Y^CnY0

   Excel._Workbook xBk;

W~\6\U+d V0

   Excel._Worksheet xSt;

0@nR{n0

   excel= new Excel.ApplicationClass();;

0MHQ8IUt0

   xBk = excel.Workbooks.Add(true);

7U |d"M3{"c#UN3S yA0

   xSt = (Excel._Worksheet)xBk.ActiveSheet;51Testing软件测试网$Ju,I D9j&l

   //51Testing软件测试网y?m6H5A"A#[

   //取得标题51Testing软件测试网v4Hm O}(y5z

   //51Testing软件测试网 nhbCn-I+d*[:m] X

   foreach(DataColumn col in dv.Table.Columns)

i\{n Q8p0

   {51Testing软件测试网\/`Z]q0Gok

    colIndex++;51Testing软件测试网b E)O] _p4w

    excel.Cells[4,colIndex] = col.ColumnName;51Testing软件测试网1tnKz#~b

    xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐51Testing软件测试网9LmV;~Zd

   }51Testing软件测试网!W-@}']5K(C Z{I'BO)p

   //

b7v Hh0?(Z_T0

   //取得表格中的数据

zJ"D3k4{a0

   //51Testing软件测试网|I.t cO#j%_

   foreach(DataRowView row in dv)

8dW&`#]err0

   {

!WK-wa)ro-]7v$d0

    rowIndex ++;

hab4cJ6@0

    colIndex = 1;51Testing软件测试网-Ft x(U*Y\

    foreach(DataColumn col in dv.Table.Columns)

#m bL-a8|'Mgg&v0

    {51Testing软件测试网rn^n0m4i;Q

     colIndex ++;51Testing软件测试网$Blsa_3q|eJ

     if(col.DataType == System.Type.GetType("System.DateTime"))

uy7@*{]0

     {

{RU*}2w@GX;Z0

      excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");51Testing软件测试网8LQ#@9}/u,vs$q

      xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐51Testing软件测试网!Ob Ps:k1B3^$L

     }

qm \Ja*@Q(D0

     else

%^4SLL`*jx0

     if(col.DataType == System.Type.GetType("System.String"))51Testing软件测试网Z*n-wJS$AA

     {51Testing软件测试网P]2e-PJ2]

      excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();51Testing软件测试网X%N`Q.n7@,^9D

      xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐

aE!c3YA-l0

     }51Testing软件测试网q kA-v R(z

     else51Testing软件测试网"l0vppAoD5l

     {

#h\Rkn fZ8h{-X0

      excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();

h \ O&n9`:^&E;m_0

     }

&k z7j[-Qk*` {0

    }

oD4LJ{8w+K0

   }

s1P7d[qR$T0

   //

BV5N7k&x-^P0

   //加载一个合计行51Testing软件测试网)|,P9gk:J

   //51Testing软件测试网7j P` j7eC

   int rowSum = rowIndex + 1;

%q/r {}M7[wY7@yi%c0

   int colSum = 2;

1}"Y4O:r6g"E&Q8`#X,G0

   excel.Cells[rowSum,2] = "合计";

[6pM9W!t0

   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;51Testing软件测试网#]to3M)ot

   //

W*fPA5?F-t+r*AO].S0

   //设置选中的部分的颜色

8bcCK5J%dMw0

   //

JfNj!Og7V0

   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();

n{ _}L`U0

   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种

@'KFrKt M-J;^0

   //51Testing软件测试网 kG+MK9bG9O*SL,k

   //取得整个报表的标题

vPU T'z4Rtt0

   //

Z aO {,X8U {1y0

   excel.Cells[2,2] = str;51Testing软件测试网6Pl[CeiQ$fM

   //51Testing软件测试网 _`~kk{M

   //设置整个报表的标题格式51Testing软件测试网z1i"v%K0@? JR4^)Q&r

   //

r+?m&`)@a [0

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;51Testing软件测试网"n9M G?P#D/Dkk"r

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;51Testing软件测试网^N#j^:B!e}

   //51Testing软件测试网+x a1W.iF])V3I\

   //设置报表表格为最适应宽度51Testing软件测试网2Xy d2D|ql[/z

   //

,P3v*@.f'w D|m$zd0

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();51Testing软件测试网y7F ?])S\k:|S q

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();51Testing软件测试网.F'Z.R$y`g,X"c

   //51Testing软件测试网4p I+],~ cv*Me.t

   //设置整个报表的标题为跨列居中51Testing软件测试网X!~Dm [ {vg

   //

g{W'u;f5d1JZZ0te0

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();51Testing软件测试网Zp|[n!xSc&j L

   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;51Testing软件测试网F"duZ V O5rpe

   //51Testing软件测试网^n;E$~E?

   //绘制边框

)g0rPQ\I'E1J0

   //

rB2M%H5TZ0

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;

eh4laN#P*u;v I2{0

   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;//设置左边线加粗

,^qP0}@Aj+j0O0

   xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;//设置上边线加粗51Testing软件测试网$^U JNave

   xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;//设置右边线加粗51Testing软件测试网2T6I FA'R

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

LV^7R^0

   //51Testing软件测试网7]*U:LmR,uC3[eN4b`

   //显示效果

@*D b Hr$?l4](|"m v%Q0

   //

uh/fx8F4J_c.j u1k0

   excel.Visible=true;

$}+Kq+r%e+E [r%tJ-k0

  }

L7WE,fd3a$Tk$S0

 }

0H ImT w` Cj6o#D0

}

Z Hyco wc"PNH0

 51Testing软件测试网(am,\BkVo;GGnL

源文档 <http://cache.baidu.com/c?word=xlvaligncenter&url=http%3A//www%2E21tx%2Ecom/dev/2003/10/03/10182%2Ehtml&b=0&a=7&user=baidu>51Testing软件测试网A:Z5@ |0t

 

-oD,a9PW J'v0
相关阅读:

TAG: 其它相关

 

评分:0

我来说两句

日历

« 2024-04-06  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 43561
  • 日志数: 61
  • 建立时间: 2007-01-15
  • 更新时间: 2007-07-23

RSS订阅

Open Toolbar