打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# Excel操作类_黑色·幽灵
C# Excel操作类
2009年08月12日 星期三 上午 00:01

原文地址:http://www.codeproject.com/office/package.asp

代码如下

using System;
using System.IO;
using System.Collections;
using System.Threading;
using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;

namespace ATPMain
{
/// <summary>
/// Project: Assay Trending Process - Controlled Spreadsheets project
/// Author:   Vahe Karamian
/// Date:   03/01/2005
/// Version: 0.0
/// </summary>
public class VkExcel
{
   private Excel.Application excelApp = null;
   private Excel.Workbook   excelWorkbook = null;
   private Excel.Sheets    excelSheets = null;
   private Excel.Worksheet   excelWorksheet = null;

   private static object vk_missing = System.Reflection.Missing.Value;

   private static object vk_visible = true;
   private static object vk_false = false;
   private static object vk_true   = true;

   private bool vk_app_visible = false;

   private object vk_filename;

   #region OPEN WORKBOOK VARIABLES
   private object vk_update_links      = 0;
   private object vk_read_only       = vk_true;
   private object vk_format        = 1;
   private object vk_password        = vk_missing;
   private object vk_write_res_password    = vk_missing;
   private object vk_ignore_read_only_recommend = vk_true;
   private object vk_origin        = vk_missing;
   private object vk_delimiter       = vk_missing;
   private object vk_editable        = vk_false;
   private object vk_notify        = vk_false;
   private object vk_converter       = vk_missing;
   private object vk_add_to_mru       = vk_false;
   private object vk_local         = vk_false;
   private object vk_corrupt_load      = vk_false;
   #endregion

   #region CLOSE WORKBOOK VARIABLES
   private object vk_save_changes = vk_false;
   private object vk_route_workbook = vk_false;
   #endregion

   /// <summary>
   /// Vahe Karamian - 03/04/2005 - Excel Object Constructor.
   /// </summary>
   public VkExcel()
   {
    this.startExcel();
   }

   /// <summary>
   /// Vahe Karamian - 03/04/2005 - Excel Object Constructor
   /// visible is a parameter, either TRUE or FALSE, of type object.
   /// </summary>
   /// <param name="visible">Visible parameter, true for visible, false for non-visible</param>
   public VkExcel(bool visible)
   {
    this.vk_app_visible = visible;
    this.startExcel();
   }

   /// <summary>
   /// Vahe Karamian - 03/04/2005 - Start Excel Application
   /// </summary>
   #region START EXCEL
   private void startExcel()
   {
    if( this.excelApp == null )
    {
     this.excelApp = new Excel.ApplicationClass();
    }
   
    // Make Excel Visible
    this.excelApp.Visible = this.vk_app_visible;
   }
   #endregion

   /// <summary>
   /// Vahe Karamian - 03/23/2005 - Kill the current Excel Process
   /// </summary>
   #region STOP EXCEL
   public void stopExcel()
   {
    if( this.excelApp != null )
    {
     Process[] pProcess;
     pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");
     pProcess[0].Kill();
    }
   }
   #endregion

   /// <summary>
   /// Vahe Karamian - 03/09/2005 - Open File function for Excel 2003
   /// The following function will take in a filename, and a password
   /// associated, if needed, to open the file.
   /// </summary>
   /// <param name="fileName"></param>
   /// <param name="password"></param>
   #region OPEN FILE FOR EXCEL
   public string OpenFile(string fileName, string password)
   {
    vk_filename = fileName;

    if( password.Length > 0 )
    {
     vk_password = password;
    }

    try
    {
     // Open a workbook in Excel
     this.excelWorkbook = this.excelApp.Workbooks.Open(
      fileName, vk_update_links, vk_read_only, vk_format, vk_password,
      vk_write_res_password, vk_ignore_read_only_recommend, vk_origin,
      vk_delimiter, vk_editable, vk_notify, vk_converter, vk_add_to_mru,
      vk_local, vk_corrupt_load);
    }
    catch(Exception e)
    {
     this.CloseFile();
     return e.Message;
    }
    return "OK";
   }
   #endregion

   public void CloseFile()
   {
    excelWorkbook.Close( vk_save_changes, vk_filename, vk_route_workbook );
   }

   /// <summary>
   /// Vahe Karamian - 03/20/2005 - Get Excel Sheets
   /// Get the collection of sheets in the workbook
   /// </summary>
   #region GET EXCEL SHEETS
   public void GetExcelSheets()
   {
    if( this.excelWorkbook != null )
    {
     excelSheets = excelWorkbook.Worksheets;
    }
   }
   #endregion

   /// <summary>
   /// Vahe Karamian - 03/21/2005 - Find Excel ATP Worksheet
   /// Search for ATP worksheet, if found return TRUE
   /// </summary>
   /// <returns>bool</returns>
   #region FIND EXCEL ATP WORKSHEET
   public bool FindExcelWorksheet(string worksheetName)
   {
    bool ATP_SHEET_FOUND = false;

    if( this.excelSheets != null )
    {
     // Step thru the worksheet collection and see if ATP sheet is
     // available. If found return true;
     for( int i=1; i<=this.excelSheets.Count; i++ )
     {
      this.excelWorksheet = (Excel.Worksheet)excelSheets.get_Item((object)i);
      if( this.excelWorksheet.Name.Equals(worksheetName) )
      {
       this.excelWorksheet.Activate();
       ATP_SHEET_FOUND = true;
       return ATP_SHEET_FOUND;
      }
     }
    }
    return ATP_SHEET_FOUND;
   }
   #endregion

   /// <summary>
   /// Vahe Karamian - 03/22/2005 - Get Range from Worksheet
   /// Return content of range from the selected range
   /// </summary>
   /// <param name="range">Range parameter: Example, GetRange("A1:D10")</param>
   #region GET RANGE
   public string[] GetRange(string range)
   {
    Excel.Range workingRangeCells = excelWorksheet.get_Range(range,Type.Missing);
    //workingRangeCells.Select();
    System.Array array = (System.Array)workingRangeCells.Cells.Value2;
    string[] arrayS = this.ConvertToStringArray(array);

    return arrayS;
   }
   #endregion

   /// <summary>
   /// Vahe Karamian - 03/22/2005 - Convert To String Array
   /// Convert System.Array into string[]
   /// </summary>
   /// <param name="values">Values from range object</param>
   /// <returns>String[]</returns>
   #region CONVERT TO STRING ARRAY
   private string[] ConvertToStringArray(System.Array values)
   {
    string[] newArray = new string[values.Length];

    int index = 0;
    for ( int i = values.GetLowerBound(0); i <= values.GetUpperBound(0); i++ )
    {
     for ( int j = values.GetLowerBound(1); j <= values.GetUpperBound(1); j++ )
     {
      if(values.GetValue(i,j)==null)
      {
       newArray[index]="";
      }
      else
      {
       newArray[index]=(string)values.GetValue(i,j).ToString();
      }
      index++;
     }
    }
    return newArray;
   }
   #endregion
}
}


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
利用java反射调用类的的私有方法
Silverlight父窗口弹出子窗口及窗口之间数据交互
WEB页获取串口数据
delegate event
Validate state with class invariants
手把手教你完成App支付JAVA后台
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服