.Net控制Excel

使用OLE的方式控制Excel读取/写入其中的数据,设定格式等等。
 /// <summary>
 /// Excel处理类
 /// </summary>
 public class ExcelHander
 {
  public ExcelHander()
  {
   //
   // TODO: Add constructor logic here
   //
  }
  private string AList="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
  public string GetAix(int x,int y)
  {
   char [] AChars=AList.ToCharArray();
   if(x>=26){return "";}
   string s="";
   s=s+AChars[x-1].ToString();
   s=s+y.ToString();
   return s;
  }
  public void setValue(int x,int y,string align,string text)
  {
   Excel.Range range=sheet.get_Range(this.GetAix(x,y),miss);
   range.set_Value(miss,text);
   if(align.ToUpper()=="CENTER")
   {
    range.HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;
   }
   if(align.ToUpper()=="LEFT")
   {
    range.HorizontalAlignment=Excel.XlHAlign.xlHAlignLeft;
   }
   if(align.ToUpper()=="RIGHT")
   {
    range.HorizontalAlignment=Excel.XlHAlign.xlHAlignRight;
   }
  }
  public void setValue(int x,int y,string text)
  {
   Excel.Range range=sheet.get_Range(this.GetAix(x,y),miss);
   range.set_Value(miss,text);
  }
  public void setValue(int x,int y,string text,System.Drawing.Font font,System.Drawing.Color color)
  {
   this.setValue(x,y,text);
   Excel.Range range=sheet.get_Range(this.GetAix(x,y),miss);
   range.Font.Size=font.Size;
   range.Font.Bold=font.Bold;
   range.Font.Color=color;
   range.Font.Name=font.Name;
   range.Font.Italic=font.Italic;
   range.Font.Underline=font.Underline;
  }
  public void insertRow(int y)
  {
   Excel.Range range=sheet.get_Range(GetAix(1,y),GetAix(25,y));
   range.Copy(miss);
   range.Insert(Excel.XlDirection.xlDown,miss);
   range.get_Range(GetAix(1,y),GetAix(25,y));
   range.Select();
   sheet.Paste(miss,miss);
  }
  public void past()
  {
   string s="a,b,c,d,e,f,g";
   sheet.Paste(sheet.get_Range(this.GetAix(10,10),miss),s);
  }
  public void setBorder(int x1,int y1,int x2,int y2,int Width)
  {
   Excel.Range range=sheet.get_Range(this.GetAix(x1,y1),this.GetAix(x2,y2));
   range.Borders.Weight=Width;
  }
  public void mergeCell(int x1,int y1,int x2,int y2)
  {
   Excel.Range range=sheet.get_Range(this.GetAix(x1,y1),this.GetAix(x2,y2));
   range.Merge(true);
  }
  
  public Excel.Range getRange(int x1,int y1,int x2,int y2)
  {
   Excel.Range range=sheet.get_Range(this.GetAix(x1,y1),this.GetAix(x2,y2));
   return range;
  }
  private Missing miss=Missing.Value; //忽略的参数OLENULL
  public static Missing MissValue=Missing.Value;
  private Excel.Application m_objExcel;//Excel应用程序实例
  private Excel.Workbooks m_objBooks;//工作表集合
  private Excel.Workbook m_objBook;//当前操作的工作表
  private Excel.Worksheet sheet;//当前操作的表格
  public Excel.Worksheet CurrentSheet
  {
   get
   {
    return sheet;
   }
   set
   {
    this.sheet=value;
   }
  }
  public Excel.Workbooks CurrentWorkBooks
  {
   get
   {
    return this.m_objBooks;
   }
   set
   {
    this.m_objBooks=value;
   }
  }
  
  public Excel.Workbook CurrentWorkBook
  {
   get
   {
    return this.m_objBook;
   }
   set
   {
    this.m_objBook=value;
   }
  }
  public void OpenExcelFile(string filename)
  {
   m_objExcel = new Excel.Application();
   UserControl(false);
   
   m_objExcel.Workbooks.Open(
    filename,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss,
    miss);
   
   m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
   
   m_objBook = m_objExcel.ActiveWorkbook;
   sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
  }
  public void UserControl(bool usercontrol)
  {
   if(m_objExcel==null){return ;}
   m_objExcel.UserControl=usercontrol;
   m_objExcel.DisplayAlerts=usercontrol;
   m_objExcel.Visible = usercontrol;
  }
  public void CreateExceFile()
  {
   m_objExcel = new Excel.Application();
   UserControl(false);
   m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
   m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
   sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
  }
  public void SaveAs(string FileName)
  {
   m_objBook.SaveAs(FileName, miss, miss, miss, miss,
    miss, Excel.XlSaveAsAccessMode.xlNoChange,
    Excel.XlSaveConflictResolution.xlLocalSessionChanges,
    miss,miss, miss, miss);
   //m_objBook.Close(false, miss, miss);
  }
  public void ReleaseExcel()
  {
   m_objExcel.Quit();
   Marshal.ReleaseComObject(m_objExcel);
   Marshal.ReleaseComObject(m_objBooks);
   Marshal.ReleaseComObject(m_objBook);
   Marshal.ReleaseComObject(sheet);
   GC.Collect();
  }
 }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值