winform導出Excel報表 2

 下面的這個類中,主要完成的功能是從數據庫中逐字段讀出數據,設置格式後,在Excel中顯示出來。  
   
  在這個類中,有兩個參數傳進來,一個是它的數據源,另一個是整個報表的標題字符串,具體看代碼就應該知道了。  
   
  using   System;  
  using   System.Data;  
  using   Excel;  
   
  namespace   LogicLayer  
  {  
    ///   <summary>  
    ///   OutputExcel   的摘要說明  
    ///   </summary>  
    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;  
      }  
    }  
  }

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