国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#調用操作Excel的一個類

2024-07-21 02:26:54
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 在這兒本來想寫長一點的文章,但因為時間的關系,沒有寫成?,F把自己做的一個小東西,c#調用excel作報表的源代碼放在這兒給大家看看。關于代碼的構成,在源代碼中已經有完整的代碼注釋了,這兒就不說什么了。

    下面的這個類中,主要完成的功能是從數據庫中逐字段讀出數據,設置格式后,在excel中顯示出來。這是它運行后的效果圖:

    在這個類中,有兩個參數傳進來,一個是它的數據源,另一個是整個報表的標題字符串,具體看代碼就應該知道了。

    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;//設置標題格式為居中對齊
    ???}

    ???//
    ???//取得表格中的數據
    ???//
    ???foreach(datarowview row in dv)
    ???{
    ????rowindex ++;
    ????colindex = 1;
    ????foreach(datacolumn col in dv.table.columns)
    ????{
    ?????colindex ++;
    ?????if(col.datatype == system.type.gettype("system.datetime"))
    ?????{
    ??????excel.cells[rowindex,colindex] = (convert.todatetime(row[col.columnname].tostring())).tostring("yyyy-mm-dd");
    ??????xst.get_range(excel.cells[rowindex,colindex],excel.cells[rowindex,colindex]).horizontalalignment = excel.xlvalign.xlvaligncenter;//設置日期型的字段格式為居中對齊
    ?????}
    ?????else
    ?????if(col.datatype == system.type.gettype("system.string"))
    ?????{
    ??????excel.cells[rowindex,colindex] = "'"+row[col.columnname].tostring();
    ??????xst.get_range(excel.cells[rowindex,colindex],excel.cells[rowindex,colindex]).horizontalalignment = excel.xlvalign.xlvaligncenter;//設置字符型的字段格式為居中對齊
    ?????}
    ?????else
    ?????{
    ??????excel.cells[rowindex,colindex] = row[col.columnname].tostring();
    ?????}
    ????}
    ???}
    ???//
    ???//加載一個合計行
    ???//
    ???int rowsum = rowindex + 1;
    ???int colsum = 2;
    ???excel.cells[rowsum,2] = "合計";
    ???xst.get_range(excel.cells[rowsum,2],excel.cells[rowsum,2]).horizontalalignment = excel.xlhalign.xlhaligncenter;
    ???//
    ???//設置選中的部分的顏色
    ???//
    ???xst.get_range(excel.cells[rowsum,colsum],excel.cells[rowsum,colindex]).select();
    ???xst.get_range(excel.cells[rowsum,colsum],excel.cells[rowsum,colindex]).interior.colorindex = 19;//設置為淺黃色,共計有56種
    ???//
    ???//取得整個報表的標題
    ???//
    ???excel.cells[2,2] = str;
    ???//
    ???//設置整個報表的標題格式
    ???//
    ???xst.get_range(excel.cells[2,2],excel.cells[2,2]).font.bold = true;
    ???xst.get_range(excel.cells[2,2],excel.cells[2,2]).font.size = 22;
    ???//
    ???//設置報表表格為最適應寬度
    ???//
    ???xst.get_range(excel.cells[4,2],excel.cells[rowsum,colindex]).select();
    ???xst.get_range(excel.cells[4,2],excel.cells[rowsum,colindex]).columns.autofit();
    ???//
    ???//設置整個報表的標題為跨列居中
    ???//
    ???xst.get_range(excel.cells[2,2],excel.cells[2,colindex]).select();
    ???xst.get_range(excel.cells[2,2],excel.cells[2,colindex]).horizontalalignment = excel.xlhalign.xlhaligncenteracrossselection;
    ???//
    ???//繪制邊框
    ???//
    ???xst.get_range(excel.cells[4,2],excel.cells[rowsum,colindex]).borders.linestyle = 1;
    ???xst.get_range(excel.cells[4,2],excel.cells[rowsum,2]).borders[excel.xlbordersindex.xledgeleft].weight = excel.xlborderweight.xlthick;//設置左邊線加粗
    ???xst.get_range(excel.cells[4,2],excel.cells[4,colindex]).borders[excel.xlbordersindex.xledgetop].weight = excel.xlborderweight.xlthick;//設置上邊線加粗
    ???xst.get_range(excel.cells[4,colindex],excel.cells[rowsum,colindex]).borders[excel.xlbordersindex.xledgeright].weight = excel.xlborderweight.xlthick;//設置右邊線加粗
    ???xst.get_range(excel.cells[rowsum,2],excel.cells[rowsum,colindex]).borders[excel.xlbordersindex.xledgebottom].weight = excel.xlborderweight.xlthick;//設置下邊線加粗
    ???//
    ???//顯示效果
    ???//
    ???excel.visible=true;
    ??}
    ?}
    }
    有人在問,如何結束excel進程?
    可以強制結束進程:)
    一般采用垃圾自動回收的技術,但為了更徹底地結束excel進程。我們可以再加上一個事件:判斷excel是否還在進程集里面。如果有的話,返回true;當然,如果沒有的話,返回false。
    當事件為真的時候,采用強制進程殺死技術就行了。
    如果要判斷excel是否在進程集里面,可以參考下面的代碼:
    ??????????? int proceedingcount = 0;
    ??????????? process[] proceddingcon = process.getprocesses();
    ??????????? foreach(process isprocedding in proceddingcon)
    ??????????? {
    ??????????????? if(isprocedding.processname == "excel")
    ??????????????? {
    ??????????????????? proceedingcount += 1;
    ??????????????? }
    ??????????? }
    ??????????? if(proceedingcount > 0)
    ??????????? {
    ??????????????? messagebox.show("該系統中已經在運行excel了。","提示",messageboxbuttons.ok,messageboxicon.information);
    return true;
    ??????????? }
    else
    {
    return false;
    }
    ??}
    ==================
    要殺死進程的話,用一個叫什么kill()的事件吧。好象是這樣的。:)


     

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 靖安县| 鹿泉市| 封丘县| 丹江口市| 乐清市| 本溪| 安图县| 宜阳县| 冕宁县| 勐海县| 米泉市| 黑龙江省| 手机| 平邑县| 闵行区| 灵寿县| 莱阳市| 伊宁县| 乌拉特后旗| 鸡东县| 黎平县| 三河市| 诸暨市| 青神县| 虎林市| 长泰县| 安丘市| 平武县| 本溪| 沁源县| 博白县| 江津市| 望谟县| 盐亭县| 泉州市| 九龙坡区| 交口县| 容城县| 苗栗市| 青神县| 浮梁县|