ExcelOperate類---基本Excel二次開發類(C#)

這是一個簡單的Excel卡發類,基於C#寫的,可以完成一些簡單的Excel操作。

/// <summary>
/// ExcelOperate 的摘要說明。Excel操作函數
/// </summary>
public class ExcelOperate
{
   private object mValue = System.Reflection.Missing.Value;

   public ExcelOperate()
   {
    //
    // TODO: 在此處添加構造函數邏輯
    //
   }

   /// <summary>
   /// 合併單元格
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
        public void Merge(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
   {
            CurSheet.get_Range(objStartCell, objEndCell).Merge(mValue); 
   }
   /// <summary>
   /// 設置連續區域的字體大小
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="strStartCell">開始單元格</param>
   /// <param name="strEndCell">結束單元格</param>
   /// <param name="intFontSize">字體大小</param>
        public void SetFontSize(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, int intFontSize) 
   {
    CurSheet.get_Range(objStartCell, objEndCell).Font.Size = intFontSize.ToString();
   }

   /// <summary>
   /// 橫向打印
   /// </summary>
   /// <param name="CurSheet"></param>
        public void xlLandscape(Microsoft.Office.Interop.Excel._Worksheet CurSheet)
   {
            CurSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;

   }
   /// <summary>
   /// 縱向打印
   /// </summary>
   /// <param name="CurSheet"></param>
        public void xlPortrait(Microsoft.Office.Interop.Excel._Worksheet CurSheet)
   {
            CurSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait;
   }
   /// <summary>
   /// 在指定單元格插入指定的值
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="Cell">單元格 如Cells[1,1]</param>
   /// <param name="objValue">文本、數字等值</param>
        public void WriteCell(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objCell, object objValue) 
   {
    CurSheet.get_Range(objCell, mValue).Value2 = objValue;

   }

   /// <summary>
   /// 在指定Range中插入指定的值
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="StartCell">開始單元格</param>
   /// <param name="EndCell">結束單元格</param>
   /// <param name="objValue">文本、數字等值</param>
        public void WriteRange(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, object objValue)
   {
    CurSheet.get_Range(objStartCell, objEndCell).Value2 = objValue;
   }
  
   /// <summary>
   /// 合併單元格,並在合併後的單元格中插入指定的值
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
   /// <param name="objValue">文本、數字等值</param>
        public void WriteAfterMerge(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, object objValue)
   {
    CurSheet.get_Range(objStartCell, objEndCell).Merge(mValue);
    CurSheet.get_Range(objStartCell, mValue).Value2 = objValue;

   }

   /// <summary>
   /// 爲單元格設置公式
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objCell">單元格</param>
   /// <param name="strFormula">公式</param>
        public void SetFormula(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objCell, string strFormula)
   {
    CurSheet.get_Range(objCell, mValue).Formula = strFormula;
   }


   /// <summary>
   /// 單元格自動換行
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
        public void AutoWrapText(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
   {
    CurSheet.get_Range(objStartCell,objEndCell).WrapText=true;
   }

   /// <summary>
   /// 設置整個連續區域的字體顏色
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
   /// <param name="clrColor">顏色</param>
        public void SetColor(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, System.Drawing.Color clrColor)
   {
    CurSheet.get_Range(objStartCell, objEndCell).Font.Color = System.Drawing.ColorTranslator.ToOle(clrColor);
   }

        /// <summary>
        /// 設置整個連續區域的單元格背景色
        /// </summary>
        /// <param name="CurSheet"></param>
        /// <param name="objStartCell"></param>
        /// <param name="objEndCell"></param>
        /// <param name="clrColor"></param>
        public void SetBgColor(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, System.Drawing.Color clrColor)
        {
            CurSheet.get_Range(objStartCell, objEndCell).Interior.Color = System.Drawing.ColorTranslator.ToOle(clrColor);
        }

   /// <summary>
   /// 設置連續區域的字體名稱
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
   /// <param name="fontname">字體名稱 隸書、仿宋_GB2312等</param>
        public void SetFontName(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, string fontname)
   {
    CurSheet.get_Range(objStartCell, objEndCell).Font.Name=fontname;
   }

   /// <summary>
   /// 設置連續區域的字體爲黑體
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
        public void SetBold(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
   {
            //CurSheet.get_Range(objStartCell, objEndCell).Font.Bold = true;
            CurSheet.get_Range(objStartCell, objEndCell).Font.Size = 12;
   }
       
  
   /// <summary>
   /// 設置連續區域的邊框:上下左右都爲黑色連續邊框
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
        public void SetBorderAll(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
   {
            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
            CurSheet.get_Range(objStartCell, objEndCell).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;

   }

   /// <summary>
   /// 設置連續區域水平居中
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
        public void SetHAlignCenter(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
   {
            CurSheet.get_Range(objStartCell, objEndCell).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
   }
   
   /// <summary>
   /// 設置連續區域水平居左
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
        public void SetHAlignLeft(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
   {
            CurSheet.get_Range(objStartCell, objEndCell).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
   }

   /// <summary>
   /// 設置連續區域水平居右
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
        public void SetHAlignRight(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
   {
            CurSheet.get_Range(objStartCell, objEndCell).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;
   }


   /// <summary>
   /// 設置連續區域的顯示格式
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
   /// <param name="strNF">如"#,##0.00"的顯示格式</param>
        public void SetNumberFormat(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, string strNF)
   {
    CurSheet.get_Range(objStartCell, objEndCell).NumberFormat = strNF;
            
   }
        public void border(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object aa, object bb)
        {
            CurSheet.get_Range(aa, bb).Borders.LineStyle = 1;
        }
  
   /// <summary>
   /// 設置列寬
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="strColID">列標識,如A代表第一列</param>
   /// <param name="dblWidth">寬度</param>
        public void SetColumnWidth(Microsoft.Office.Interop.Excel._Worksheet CurSheet, string strColID, double dblWidth)
   {
            ((Microsoft.Office.Interop.Excel.Range)CurSheet.Columns.GetType().InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, CurSheet.Columns, new object[] { (strColID + ":" + strColID).ToString() })).ColumnWidth = dblWidth;
   }

   /// <summary>
   /// 設置列寬
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
   /// <param name="dblWidth">寬度</param>
        public void SetColumnWidth(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, double dblWidth)
   {
    CurSheet.get_Range(objStartCell,objEndCell).ColumnWidth=dblWidth;
   }


   /// <summary>
   /// 設置行高
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objStartCell">開始單元格</param>
   /// <param name="objEndCell">結束單元格</param>
   /// <param name="dblHeight">行高</param>
        public void SetRowHeight(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objStartCell, object objEndCell, double dblHeight)
   {
    CurSheet.get_Range(objStartCell,objEndCell).RowHeight=dblHeight;
            
   }

  
   /// <summary>
   /// 爲單元格添加超級鏈接
   /// </summary>
   /// <param name="CurSheet">Worksheet</param>
   /// <param name="objCell">單元格</param>
   /// <param name="strAddress">鏈接地址</param>
   /// <param name="strTip">屏幕提示</param>
   /// <param name="strText">鏈接文本</param>
        public void AddHyperLink(Microsoft.Office.Interop.Excel._Worksheet CurSheet, object objCell, string strAddress, string strTip, string strText)
   {
    CurSheet.Hyperlinks.Add(CurSheet.get_Range(objCell, objCell),strAddress, mValue, strTip, strText);
   }

   /// <summary>
   /// 另存爲xls文件
   /// </summary>
   /// <param name="CurBook">Workbook</param>
   /// <param name="strFilePath">文件路徑</param>
        public void Save(Microsoft.Office.Interop.Excel._Workbook CurBook, string strFilePath)
   {
    CurBook.SaveCopyAs(strFilePath);
   }

   /// <summary>
   /// 保存文件
   /// </summary>
   /// <param name="CurBook">Workbook</param>
   /// <param name="strFilePath">文件路徑</param>
        public void SaveAs(Microsoft.Office.Interop.Excel._Workbook CurBook, string strFilePath)
   {
            CurBook.SaveAs(strFilePath, mValue, mValue, mValue, mValue, mValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, mValue, mValue, mValue, mValue, mValue);
   }

   /// <summary>
   /// 另存爲html文件
   /// </summary>
   /// <param name="CurBook">Workbook</param>
   /// <param name="strFilePath">文件路徑</param>
        public void SaveHtml(Microsoft.Office.Interop.Excel._Workbook CurBook, string strFilePath)
   {
            CurBook.SaveAs(strFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml, mValue, mValue, mValue, mValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, mValue, mValue, mValue, mValue, mValue);
   }


   /// <summary>
   /// 釋放內存
   /// </summary>
        public void Dispose(Microsoft.Office.Interop.Excel._Worksheet CurSheet, Microsoft.Office.Interop.Excel._Workbook CurBook, Microsoft.Office.Interop.Excel._Application CurExcel)
   {
    try
    {
     System.Runtime.InteropServices.Marshal.ReleaseComObject(CurSheet);
     CurSheet = null;
     CurBook.Close(false, mValue, mValue);
     System.Runtime.InteropServices.Marshal.ReleaseComObject(CurBook);
     CurBook = null;

     CurExcel.Quit();
     System.Runtime.InteropServices.Marshal.ReleaseComObject(CurExcel);
     CurExcel = null;
           
     GC.Collect();
     GC.WaitForPendingFinalizers();
    }
    catch(System.Exception ex)
    {
        HttpContext.Current.Response.Write( "在釋放Excel內存空間時發生了一個錯誤:"+ex);
    }
    finally
    {
     foreach(System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("Excel"))                    
                 //if (pro.StartTime < DateTime.Now)
                 pro.Kill();
            }
            System.GC.SuppressFinalize(this);

   }
}

另:Excel二次開發一些簡單用法。

//建立一個Excel.Application的新進程
                    Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                    if (app == null)
                    {
                        return null;
                    }
                    app.Visible = false;
                    app.UserControl = true;
                    Workbooks workbooks = app.Workbooks;
                    _Workbook workbook = workbooks.Add(template_path + "");
                    Sheets sheets = workbook.Worksheets;
                    _Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
                    if (worksheet == null)
                    {
                        return null;
                    }

Range rng1 = worksheet.get_Range("A1", Type.Missing);
                    rng1.Value2 = “your content”;

workbook.SaveAs(save_path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    excelOperate.Dispose(worksheet, workbook, app);//關閉Excel進程

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