一个 问题 急急啊 向excel里面插入一张图片,,急急 啊在线等!!救命救命啊 真的各位大虾要帮小第一把!!!
问题是我向excel里面插入一张图片是可以实现的
但是我从数据库中读出
向excel里面循环插入图片就有错误 还有个问题在asp.net 怎么设置excel行的高度,列的宽度...
小弟万分感谢啊....救命了真的是救命啊
错误是
异常详细信息: System.Runtime.InteropServices.COMException: 类 Pictures 的 Insert 方法无效
源错误:
行 64: m_objRange.Select();
行 65: Excel.Pictures pics = (Excel.Pictures)m_objSheet.Pictures(m_objOpt);
行 66: pics.Insert(PicturePath, m_objOpt);
行 67: }
行 68:
代码在附上:
string filename = @"d:\ad.xls";
SqlConnection conn = new SqlConnection("server=.;database=qipeiexcel;uid=sa");
string sql = "select a.id,a.oem,b.smt_pic as ap_pic,b.smt_cpname,b.smt_cpjm, a.price, a.[count],a.total,"+
"a.beizhu,a.weizhi from allstafforder as a,"+
"(select id,ap_oem,smt_pic,smt_cpname,smt_cpjm from tn8c8p8 where "+
"smt_yp_id=(select id from tn8c8y8 where [user]='fushan') ) "+
"as b where a.oem=b.smt_cpname and a.sid='52' and a.state=1 and a.[user]='fushan' "+
"order by a.weizhi ";
DataTable dt4=new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
adapter.Fill(dt4);
Class1 c=new Class1();
for(int j=0;j<dt4.Rows.Count;j++)
{
//objApp.Cells[j+16,2]=dt4.Rows[j]["ap_pic"].ToString();
c.Open(filename);
int a=j+16;
c.InsertPicture("b6" ,@"http:\\192.168.1.104\sdfsdf\p\"+dt4.Rows[j]["ap_pic"].ToString());
c.SaveFile(filename);
c.Dispose();
// objApp.Cells[j+16,3]=dt4.Rows[j]["smt_cpname"].ToString();
// objApp.Cells[j+16,4]=dt4.Rows[j]["smt_cpjm"].ToString();
// objApp.Cells[j+16,5]=dt4.Rows[j]["beizhu"].ToString();
// objApp.Cells[j+16,6]=dt4.Rows[j]["price"].ToString();
// objApp.Cells[j+16,7]=dt4.Rows[j]["count"].ToString();
// objApp.Cells[j+16,8]=dt4.Rows[j]["total"].ToString();
}
//****************************************
public class Class1
{
private Excel.Application m_objExcel = null;
private Excel.Workbooks m_objBooks = null;
private Excel._Workbook m_objBook = null;
private Excel.Sheets m_objSheets = null;
private Excel._Worksheet m_objSheet = null;
private Excel.Range m_objRange = null;
private object m_objOpt = System.Reflection.Missing.Value;
public Class1()
{
}
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("保存完毕!");
// }
/// <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 PictuteWidth, 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, PictuteWidth, PictureHeight);
}
/// <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();
}
/// <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; }
}
}
--------------------编程问答-------------------- 兄弟们怎么没有人顶啊
我自己先顶一下 --------------------编程问答-------------------- 咋就没有人顶拉呢 --------------------编程问答-------------------- 米人顶
已经解决了 --------------------编程问答-------------------- 怎么解决的 --------------------编程问答-------------------- 你连分都不给还顶,
补充:.NET技术 , ASP.NET