C#調用操作Excel的一個類

在這兒本來想寫長一點的文章,但因爲時間的關係,沒有寫成。現把自己做的一個小東西,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()的事件吧。好象是這樣的。:)

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章