您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關C#中怎么向Excel插入數據,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
調用方法:
MengXianhui.Utility.ExcelReport.InsertPictureToExcelipt=
newMengXianhui.Utility.ExcelReport.InsertPictureToExcel();ipt.Open();
ipt.InsertPicture("B2",@"C:\Excellogo.gif");
ipt.InsertPicture("B8",@"C:\Excellogo.gif",120,80);
ipt.SaveFile(@"C:\ExcelTest.xls");
ipt.Dispose();
簡單包裝的類:
usingSystem;
usingSystem.Windows.Forms;
usingExcel=Microsoft.Office.Interop.Excel;
namespaceMengXianhui.Utility.ExcelReport
{
classInsertPictureToExcel
{
///<summary>
///打開沒有模板的操作。
///</summary>
publicvoidOpen()
{
this.Open(String.Empty);
}
///<summary>
///功能:實現Excel應用程序的打開
///</summary>
///<paramnameparamname="TemplateFilePath">模板文件物理路徑</param>
publicvoidOpen(stringTemplateFilePath)
{
//打開對象
m_objExcel=newExcel.Application();
m_objExcel.Visible=false;
m_objExcel.DisplayAlerts=false;
if(m_objExcel.Version!="11.0")
{
MessageBox.Show("您的Excel版本不是11.0(Office2003),操作可能會出現問題。");
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+=newExcel.
AppEvents_WorkbookBeforeCloseEventHandler(m_objExcel_WorkbookBeforeClose);}
privatevoidm_objExcel_WorkbookBeforeClose(Excel.Workbookm_objBooks,refbool_Cancel)
{
MessageBox.Show("保存完畢!");
}
///<summary>
///將圖片插入到指定的單元格位置。
///注意:圖片必須是絕對物理路徑
///</summary>
///<paramnameparamname="RangeName">單元格名稱,例如:B4</param>
///<paramnameparamname="PicturePath">要插入圖片的絕對路徑。</param>
publicvoidInsertPicture(stringRangeName,stringPicturePath)
{
m_objRange=m_objSheet.get_Range(RangeName,m_objOpt);
m_objRange.Select();
Excel.Picturespics=(Excel.Pictures)m_objSheet.Pictures(m_objOpt);
pics.Insert(PicturePath,m_objOpt);
}
///<summary>
///將圖片插入到指定的單元格位置,并設置圖片的寬度和高度。
///注意:圖片必須是絕對物理路徑
///</summary>
///<paramnameparamname="RangeName">單元格名稱,例如:B4</param>
///<paramnameparamname="PicturePath">要插入圖片的絕對路徑。</param>
///<paramnameparamname="PictuteWidth">插入后,圖片在Excel中顯示的寬度。</param>
///<paramnameparamname="PictureHeight">插入后,圖片在Excel中顯示的高度。</param>
publicvoidInsertPicture
(stringRangeName,stringPicturePath,floatPictuteWidth,floatPictureHeight){
m_objRange=m_objSheet.get_Range(RangeName,m_objOpt);
m_objRange.Select();
floatPicLeft,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,
PictuteWidth,PictureHeight);}
///<summary>
///將Excel文件保存到指定的目錄,目錄必須事先存在,文件名稱不一定要存在。
///</summary>
///<paramnameparamname="OutputFilePath">要保存成的文件的全路徑。</param>
publicvoidSaveFile(stringOutputFilePath)
{
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();
}
///<summary>
///關閉應用程序
///</summary>
privatevoidClose()
{
m_objBook.Close(false,m_objOpt,m_objOpt);
m_objExcel.Quit();
}
///<summary>
///釋放所引用的COM對象。注意:這個過程一定要執行。
///</summary>
publicvoidDispose()
{
ReleaseObj(m_objSheets);
ReleaseObj(m_objBook);
ReleaseObj(m_objBooks);
ReleaseObj(m_objExcel);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
///<summary>
///釋放對象,內部調用
///</summary>
///<paramnameparamname="o"></param>
privatevoidReleaseObj(objecto)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch{}
finally{o=null;}
}
privateExcel.Applicationm_objExcel=null;
privateExcel.Workbooksm_objBooks=null;
privateExcel._Workbookm_objBook=null;
privateExcel.Sheetsm_objSheets=null;
privateExcel._Worksheetm_objSheet=null;
privateExcel.Rangem_objRange=null;
privateobjectm_objOpt=System.Reflection.Missing.Value;
}
}
以上就是C#中怎么向Excel插入數據,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。