在webapplication中读取Excel中的图片

sunhahaha2 2010-02-02 04:06:25
下面的代码, 实现从excel中读取信息的功能.
其中, 读取Excel中的图片的功能(代码中注释掉的部分), 在Windows application下可以正常运行,
但在Web application中却有错误, 错误信息是IDataObject iData = Clipboard.GetDataObject();中的iData为null.
请问这是什么原因?
1) 是因为web application无法调用剪贴版? 如果是, 下面的代码该如何改写?
2) 还是权限没设置好? 如果是的话, 如何为web application添加相关操作的权限?

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using Excel;
using System.Windows.Forms;
using System.Drawing;

namespace TestExcel
{
public partial class _ReadExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblResult.Text = string.Empty;
LoadExcel();
}

private void LoadExcel()
{
string folderPath = Server.MapPath("~/App_data/");
string fileName = folderPath + "MenuTemplate.xls";

Excel.Application eApp = null;
Excel.Workbook eBook = null;
Excel.Worksheet eSheet = null;
int startRow = 2;
object Nothing = Type.Missing;

eApp = new Excel.Application();
eBook = eApp.Workbooks.Open(fileName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);

for (int sheetIndex = 1; sheetIndex <= eBook.Worksheets.Count; sheetIndex++)
{
eSheet = (Excel.Worksheet)eBook.Worksheets[sheetIndex];
if (eSheet.UsedRange.Rows.Count <= 1)
continue;

string sheetName = eSheet.Name;
lblResult.Text += System.Environment.NewLine + System.Environment.NewLine + "Sheet Name:" + sheetName;
for (int rowIndex = startRow; rowIndex <= eSheet.UsedRange.Rows.Count; rowIndex++)
{
string menuName = ((Excel.Range)eSheet.Cells[rowIndex, 'A' - 'A' + rowIndex]).Text.ToString();
if (menuName == "")
continue;
string description = ((Excel.Range)eSheet.Cells[rowIndex, 'B' - 'A' + rowIndex]).Text.ToString();

lblResult.Text += System.Environment.NewLine + "MenuName Description";
lblResult.Text += System.Environment.NewLine + menuName + " " + description;

////Can only do it in windows application. Need more research
////Read Image and save to disk
//if (eSheet.Shapes.Count > rowIndex - startRow)
//{
// Excel.Shape s = eSheet.Shapes.Item(rowIndex - startRow + 1) as Excel.Shape;
// s.CopyPicture(Appearance.Button, Excel.XlCopyPictureFormat.xlBitmap); //Coty to memory
// IDataObject iData = Clipboard.GetDataObject();
// if (iData.GetDataPresent(DataFormats.Bitmap))
// {
// System.Drawing.Bitmap bit = (Bitmap)iData.GetData(DataFormats.Bitmap);
// bit.Save(folderPath + rowIndex.ToString() + ".jpg");
// bit.Dispose();
// }
//}
}
}

eApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(eSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(eBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(eApp);
GC.Collect();
}
}
}
...全文
128 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aprillee87 2012-05-03
  • 打赏
  • 举报
回复
怎么解决的?能共享下么
KILLKEY 2010-03-11
  • 打赏
  • 举报
回复
我现在也需要用这个功能,能告诉怎么做的吗?? 加您qq了 给您留言了!! 这个弄的我头都大了!! 解决不胜感激
jinliang2010 2010-02-06
  • 打赏
  • 举报
回复
换个思路,用OWC绘图……
tzs2304 2010-02-06
  • 打赏
  • 举报
回复
jf
sunhahaha2 2010-02-03
  • 打赏
  • 举报
回复
晕了, 没人回复还结不了, 有人来接分不?
sunhahaha2 2010-02-03
  • 打赏
  • 举报
回复
自己研究解决了. 结了.
sunhahaha2 2010-02-03
  • 打赏
  • 举报
回复
顶一下, 有高人研究过吗?

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧