本篇文章給大家分享的是有關(guān)C#中怎么向Excel插入數(shù)據(jù),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
海東網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,海東網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為海東1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的海東做網(wǎng)站的公司定做!
調(diào)用方法:
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();
簡(jiǎn)單包裝的類:
usingSystem;
usingSystem.Windows.Forms;
usingExcel=Microsoft.Office.Interop.Excel;
namespaceMengXianhui.Utility.ExcelReport
{
classInsertPictureToExcel
{
///
///打開沒(méi)有模板的操作。
///
publicvoidOpen()
{
this.Open(String.Empty);
}
///
///功能:實(shí)現(xiàn)Excel應(yīng)用程序的打開
///
///<paramnameparamname="TemplateFilePath">模板文件物理路徑
publicvoidOpen(stringTemplateFilePath)
{
//打開對(duì)象
m_objExcel=newExcel.Application();
m_objExcel.Visible=false;
m_objExcel.DisplayAlerts=false;
if(m_objExcel.Version!="11.0")
{
MessageBox.Show("您的Excel版本不是11.0(Office2003),操作可能會(huì)出現(xiàn)問(wèn)題。");
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("保存完畢!");
}
///
///將圖片插入到指定的單元格位置。
///注意:圖片必須是絕對(duì)物理路徑
///
///<paramnameparamname="RangeName">單元格名稱,例如:B4
///<paramnameparamname="PicturePath">要插入圖片的絕對(duì)路徑。
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);
}
///
///將圖片插入到指定的單元格位置,并設(shè)置圖片的寬度和高度。
///注意:圖片必須是絕對(duì)物理路徑
///
///<paramnameparamname="RangeName">單元格名稱,例如:B4
///<paramnameparamname="PicturePath">要插入圖片的絕對(duì)路徑。
///<paramnameparamname="PictuteWidth">插入后,圖片在Excel中顯示的寬度。
///<paramnameparamname="PictureHeight">插入后,圖片在Excel中顯示的高度。
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);
//參數(shù)含義:
//圖片路徑
//是否鏈接到文件
//圖片插入時(shí)是否隨文檔一起保存
//圖片在文檔中的坐標(biāo)位置(單位:points)
//圖片顯示的寬度和高度(單位:points)
//參數(shù)詳細(xì)信息參見(jiàn):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);}
///
///將Excel文件保存到指定的目錄,目錄必須事先存在,文件名稱不一定要存在。
///
///<paramnameparamname="OutputFilePath">要保存成的文件的全路徑。
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();
}
///
///關(guān)閉應(yīng)用程序
///
privatevoidClose()
{
m_objBook.Close(false,m_objOpt,m_objOpt);
m_objExcel.Quit();
}
///
///釋放所引用的COM對(duì)象。注意:這個(gè)過(guò)程一定要執(zhí)行。
///
publicvoidDispose()
{
ReleaseObj(m_objSheets);
ReleaseObj(m_objBook);
ReleaseObj(m_objBooks);
ReleaseObj(m_objExcel);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
///
///釋放對(duì)象,內(nèi)部調(diào)用
///
///<paramnameparamname="o">
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插入數(shù)據(jù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。