注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

麦子的家

要做的事情总找得出时间和机会 不要做的事情总找得出藉口

 
 
 

日志

 
 

C# 从Excel里提取图片并保存到本地  

2012-09-27 17:33:58|  分类: 麦子的技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Excel;

namespace Excel.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private string exclePath = @"E:\MyFile\材料.xls";
        private int StartRow = 1;    //读的起始行
        protected void btnExcel_Click(object sender, System.EventArgs e)
        {
            Thread cbThread = new Thread(new ThreadStart(CopyExcel));
            cbThread.TrySetApartmentState(ApartmentState.STA);    //指定单线程,否则无法从剪贴板中读取数据
            cbThread.Start();
        }

        protected void CopyExcel()
        {
            Excel.Application excel = new Excel.Application();//引用Excel对象
            Excel.Workbook workbook = excel.Workbooks.Add(exclePath);
            excel.UserControl = true;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            excel.Visible = false;
            for (int i = 0; i < workbook.Worksheets.Count; i++)//循环取所有的Sheet.
            {
                Excel.Worksheet sheet = workbook.Worksheets.get_Item(i + 1) as Excel.Worksheet;//从1开始.
                for (int row = StartRow; row <= sheet.UsedRange.Rows.Count; row++)
                {
                    //取单元格值;
                    for (int col = 1; col <= sheet.UsedRange.Columns.Count; col++)
                    {
                        Excel.Range range = sheet.Cells[row, col] as Excel.Range;
                        sb.Append("," + col.ToString() + ":" + range.Text);
                    }
                    sb.Append(System.Environment.NewLine);
                    //取存图片;
                    if (sheet.Shapes.Count > row - StartRow)
                    {
                        Excel.Shape s = sheet.Shapes.Item(row - StartRow + 1) as Excel.Shape;
                        Clipboard.Clear();
                        s.CopyPicture(Appearance.Button, Excel.XlCopyPictureFormat.xlBitmap); //COPY到内存。
                        IDataObject iData = Clipboard.GetDataObject();

                        if (iData != null && iData.GetDataPresent(DataFormats.Bitmap))
                        {
                            System.Drawing.Image img = Clipboard.GetImage();  //从内存读取图片
                            if (img != null)
                            {
                                img.Save(@"E:\\aa.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);   //保存到本地
                            }
                        }
                        else
                        {
                           
                        }
                    }
                }
            }
            workbook.Close(false, null, null);
            excel.Quit();
        }
    }
}
  评论这张
 
阅读(3351)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018