这个类可以很好的控制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();
}
}
/// <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();
}
}