这几天没事在网上闲逛的时候看到类似的代码,花了点时间测试了一下。觉得不错。简单的整理一下。
在项目中需要引用:COM:Microsoft Office 11.0 Object Library
COM:Microsoft Excel 11.0 Object Library
简单封装的代码类:
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace Northsnow.ExportExcel
... {
class InsertPictureToExcel
...{
/**//// <summary>
/// 打开没有模板的操作。
/// </summary>
public void Open()
...{
this.Open(String.Empty); //调用另一个构造器
}
/**//// <summary>
/// 功能:实现Excel应用程序的打开
/// </summary>
/// <param name="TemplateFilePath">模板文件物理路径</param>
public void Open(string TemplateFilePath)
...{
//打开对象
m_objExcel = new Excel.Application();
m_objExcel.Visible = false;
m_objExcel.DisplayAlerts = false;
if (m_objExcel.Version != "11.0")
...{
MessageBox.Show("您的 Excel 版本不是 11.0 (Office 2003),操作可能会出现问题。");
m_objExcel.Quit();
return;
}
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
if (TemplateFilePath.Equals(String.Empty))
...{
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
}
else
...{
m_objBook = m_objBooks.Open(TemplateFilePath, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
}
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
m_objExcel.WorkbookBeforeClose += new Excel.AppEvents_WorkbookBeforeCloseEventHandler(m_objExcel_WorkbookBeforeClose); //事件委托
}
//关闭前事件调用的程序
private void m_objExcel_WorkbookBeforeClose(Excel.Workbook m_objBooks, ref bool _Cancel)
...{
MessageBox.Show("保存完毕!");
}
public void setColumnWidth(object cIndex, long lWidth)
...{
Excel.Range objRange = (Excel.Range) m_objSheet.Cells.get_Item(1,cIndex);
objRange.ColumnWidth = lWidth;
}
public void setRowHeight(object rIndex, long lHeight)
...{
Excel.Range objRange = (Excel.Range)m_objSheet.Cells.get_Item(rIndex, 1);
objRange.RowHeight = lHeight;
}
public void InsertValue(object oContent, object rIndex, object cIndex,object fSize,Boolean fBold,Boolean fItalic)
...{
m_objSheet.Cells[rIndex, cIndex] = oContent ;
m_objRange=(Excel.Range) m_objSheet.Cells.get_Item(rIndex, cIndex);
m_objRange.Font.Bold = fBold ;
m_objRange.Font.Italic = fItalic;
m_objRange.Font.Size = fSize;
//Excel.Range aa = m_objSheet.get_Range(m_objSheet.Cells.get_Item(6, 6), m_objSheet.Cells.get_Item(7, 7));
}
/**//// <summary>
/// 将图片插入到指定的单元格位置。
/// 注意:图片必须是绝对物理路径
/// </summary>
/// <param name="RangeName">单元格名称,例如:B4</param>
/// <param name="PicturePath">要插入图片的绝对路径。</param>
public void InsertPicture(string RangeName, string PicturePath)
...{
m_objRange = m_objSheet.get_Range(RangeName, m_objOpt);
m_objRange.Select();
Excel.Pictures pics = (Excel.Pictures)m_objSheet.Pictures(m_objOpt);
pics.Insert(PicturePath, m_objOpt);
}
/**//// <summary>
/// 将图片插入到指定的单元格位置,并设置图片的宽度和高度。
/// 注意:图片必须是绝对物理路径
/// </summary>
/// <param name="RangeName">单元格名称,例如:B4</param>
/// <param name="PicturePath">要插入图片的绝对路径。</param>
/// <param name="PictuteWidth">插入后,图片在Excel中显示的宽度。</param>
/// <param name="PictureHeight">插入后,图片在Excel中显示的高度。</param>
public void InsertPicture(string RangeName, string PicturePath, float PictureWidth, float PictureHeight)
...{
m_objRange = m_objSheet.get_Range(RangeName, m_objOpt);
m_objRange.Select();
float PicLeft, PicTop;
PicLeft = Convert.ToSingle(m_objRange.Left);
PicTop = Convert.ToSingle(m_objRange.Top);
//参数含义:
//图片路径
//是否链接到文件
//图片插入时是否随文档一起保存
//图片在文档中的坐标位置(单位:points)
//图片显示的宽度和高度(单位:points)
//参数详细信息参见:http://msdn2.microsoft.com/zh-cn/library/aa221765(office.11).aspx
m_objSheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, PictureWidth, PictureHeight);
}
/**//// <summary>
/// 插入一个textbox到指定的位置(x,y)
/// </summary>
/// <param name="strContent">textbox的内容</param>
/// <param name="x">位置x</param>
/// <param name="y">位置y</param>
public void InsertTextBox(string strContent, double x, double y)
...{
Excel.TextBoxes objTxts = (Excel.TextBoxes)m_objSheet.TextBoxes(m_objOpt);
objTxts.Add(x, y, 0, 0);
Excel.TextBox objTxt = (Excel.TextBox)objTxts.Item(objTxts.Count);
objTxt.Text = strContent;
objTxt.AutoSize = true;
}
/**//// <summary>
/// 插入一个textbox到指定的位置(x,y)
/// </summary>
/// <param name="strContent">textbox的内容</param>
/// <param name="x">位置x</param>
/// <param name="y">位置y</param>
/// <param name="w">宽度w</param>
/// <param name="h">高度h</param>
public void InsertTextBox(string strContent, double x, double y, double w, double h)
...{
Excel.TextBoxes objTxts = (Excel.TextBoxes)m_objSheet.TextBoxes(m_objOpt);
objTxts.Add(x, y, w, h);
Excel.TextBox objTxt = (Excel.TextBox)objTxts.Item(objTxts.Count);
objTxt.Text = strContent;
}
/**//// <summary>
/// 将Excel文件保存到指定的目录,目录必须事先存在,文件名称不一定要存在。
/// </summary>
/// <param name="OutputFilePath">要保存成的文件的全路径。</param>
public void SaveFile(string OutputFilePath)
...{
m_objBook.SaveAs(OutputFilePath, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
this.Close();
}
关闭释放#region 关闭释放
/**//// <summary>
/// 关闭应用程序
/// </summary>
private void Close()
...{
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
}
/**//// <summary>
/// 释放所引用的COM对象。注意:这个过程一定要执行。
/// </summary>
public void Dispose()
...{
ReleaseObj(m_objSheets);
ReleaseObj(m_objBook);
ReleaseObj(m_objBooks);
ReleaseObj(m_objExcel);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
/**//// <summary>
/// 释放对象,内部调用
/// </summary>
/// <param name="o"></param>
private void ReleaseObj(object o)
...{
try
...{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch ...{ }
finally ...{ o = null; }
}
#endregion 关闭释放
定义变量#region 定义变量
private Excel.Application m_objExcel = null;
private Excel.Workbooks m_objBooks = null;
private Excel._Workbook m_objBook = null;
private Excel.Sheets m_objSheets = null;
public Excel._Worksheet m_objSheet = null;
private Excel.Range m_objRange = null;
private object m_objOpt = System.Reflection.Missing.Value;
#endregion 定义变量
}
}
调用事例:
Northsnow.ExportExcel.InsertPictureToExcel ipt
=
new
Northsnow.ExportExcel.InsertPictureToExcel();
ipt.Open( @" d:ExcelTest.xls " );
ipt.setColumnWidth( 1 , 5 );
ipt.setColumnWidth( 2 , 10 );
ipt.setColumnWidth( 3 , 10 );
ipt.setColumnWidth( 4 , 10 );
ipt.setColumnWidth( 5 , 10 );
ipt.setColumnWidth( 6 , 10 );
ipt.setRowHeight( 2 , 50 );
ipt.InsertPicture( " B2 " , @" d:a.bmp " , 20 , 20 );
ipt.InsertValue( " 塞 北 的 雪 家 庭 收 支 年 报 表 (2006) " , 2 , 3 , 16 , true , false );
ipt.InsertValue( " 收入 " , 3 , 4 , 14 , true , false );
ipt.InsertValue( " 支出 " , 3 , 6 , 14 , true , false );
ipt.InsertValue( " 结余 " , 3 , 8 , 14 , true , false );
ipt.InsertValue( " 第一季度 " , 4 , 2 , 14 , true , false );
ipt.InsertValue( " 第二季度 " , 7 , 2 , 14 , true , false );
ipt.InsertValue( " 第三季度 " , 9 , 2 , 14 , true , false );
ipt.InsertValue( " 第四季度 " , 11 , 2 , 14 , true , false );
ipt.InsertValue( " 合计 " , 13 , 2 , 14 , true , false );
ipt.InsertValue( 13410 , 4 , 4 , 12 , false , false );
ipt.InsertValue( 3410 , 4 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 4 , 8 , 12 , false , false );
ipt.InsertValue( 14410 , 7 , 4 , 12 , false , false );
ipt.InsertValue( 4410 , 7 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 7 , 8 , 12 , false , false );
ipt.InsertValue( 15410 , 9 , 4 , 12 , false , false );
ipt.InsertValue( 5410 , 9 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 9 , 8 , 12 , false , false );
ipt.InsertValue( 16410 , 11 , 4 , 12 , false , false );
ipt.InsertValue( 6410 , 11 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 11 , 8 , 12 , false , false );
ipt.InsertValue( 59640 , 13 , 4 , 12 , false , false );
ipt.InsertValue( 19640 , 13 , 6 , 12 , false , false );
ipt.InsertValue( 40000 , 13 , 8 , 12 , false , false );
// ipt.InsertTextBox("Northsnow is the most stupid person in the world", 1, 1, 400, 20);
// ipt.InsertValue("Northsnow", 3, "D",16,false,true );
// ipt.InsertValue(34.45, 3, 5,14,true,false );
ipt.SaveFile( @" d:ExcelTest.xls " );
ipt.Dispose();
ipt.Open( @" d:ExcelTest.xls " );
ipt.setColumnWidth( 1 , 5 );
ipt.setColumnWidth( 2 , 10 );
ipt.setColumnWidth( 3 , 10 );
ipt.setColumnWidth( 4 , 10 );
ipt.setColumnWidth( 5 , 10 );
ipt.setColumnWidth( 6 , 10 );
ipt.setRowHeight( 2 , 50 );
ipt.InsertPicture( " B2 " , @" d:a.bmp " , 20 , 20 );
ipt.InsertValue( " 塞 北 的 雪 家 庭 收 支 年 报 表 (2006) " , 2 , 3 , 16 , true , false );
ipt.InsertValue( " 收入 " , 3 , 4 , 14 , true , false );
ipt.InsertValue( " 支出 " , 3 , 6 , 14 , true , false );
ipt.InsertValue( " 结余 " , 3 , 8 , 14 , true , false );
ipt.InsertValue( " 第一季度 " , 4 , 2 , 14 , true , false );
ipt.InsertValue( " 第二季度 " , 7 , 2 , 14 , true , false );
ipt.InsertValue( " 第三季度 " , 9 , 2 , 14 , true , false );
ipt.InsertValue( " 第四季度 " , 11 , 2 , 14 , true , false );
ipt.InsertValue( " 合计 " , 13 , 2 , 14 , true , false );
ipt.InsertValue( 13410 , 4 , 4 , 12 , false , false );
ipt.InsertValue( 3410 , 4 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 4 , 8 , 12 , false , false );
ipt.InsertValue( 14410 , 7 , 4 , 12 , false , false );
ipt.InsertValue( 4410 , 7 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 7 , 8 , 12 , false , false );
ipt.InsertValue( 15410 , 9 , 4 , 12 , false , false );
ipt.InsertValue( 5410 , 9 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 9 , 8 , 12 , false , false );
ipt.InsertValue( 16410 , 11 , 4 , 12 , false , false );
ipt.InsertValue( 6410 , 11 , 6 , 12 , false , false );
ipt.InsertValue( 10000 , 11 , 8 , 12 , false , false );
ipt.InsertValue( 59640 , 13 , 4 , 12 , false , false );
ipt.InsertValue( 19640 , 13 , 6 , 12 , false , false );
ipt.InsertValue( 40000 , 13 , 8 , 12 , false , false );
// ipt.InsertTextBox("Northsnow is the most stupid person in the world", 1, 1, 400, 20);
// ipt.InsertValue("Northsnow", 3, "D",16,false,true );
// ipt.InsertValue(34.45, 3, 5,14,true,false );
ipt.SaveFile( @" d:ExcelTest.xls " );
ipt.Dispose();
运行后产生的 excel文件:
参考:http://blog.csdn.net/net_lover/archive/2007/07/23/1702797.aspx