把图片插入excel表格并按分类生成sheets

private void Excel_Click(object sender, System.EventArgs e)
  { 
   
   if(this.Customers_Name_List.SelectedValue!=""&&this.VersionName.SelectedValue!="")
   {
    
    Object refmissing = System.Reflection.Missing.Value;
    Excel._Application application=new Excel.ApplicationClass();
    application.Visible=false;
    
    Excel.Workbooks workbooks=application.Workbooks;
    workbooks._Open(Server.MapPath("Excel//Out.xls"), refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing);
    application.DisplayAlerts=false;
    common com=new common();
    string names="";
    names=names+"select distinct Category_Name  from Product";
    names=names+" inner join Categroy on Categroy.CategoryId=Product.CategroyID";
    names=names+" inner join Quotation on Product.ProductID=Quotation.ProductID";
    names=names+" inner join Customer on Customer.CustomerID=Quotation.CustomerID";
    names=names+" left outer join Wattage on Wattage.WattageID=Product.WattageID";
    names=names+" left outer  join MaxWattage on MaxWattage.MaxWattageID=Product.MaxWattageID";
    names=names+" left outer  join Brand on Brand.BrandID=Product.BrandID";
    names=names+" left outer  join Voltage on Voltage.VoltageID=Product.VoltageID";
    names=names+" left outer  join Product_ESL on Product_ESL.Product_ESLID=Product.Product_ESLID";
    names=names+" left outer  join Plug on Plug.PlugID=Product.PlugID";
    names=names+" left outer  join Certificate on Certificate.CertificateID=Product.CertificateID";
    names=names+" left outer  join Standard on Standard.StandardID=Product.StandardID";
    names=names+" left outer  join LampBase on LampBase.LampBaseID=Product.LampBaseID";
    names=names+" left outer  join LifeTime on LifeTime.LifeTimeID=Product.LifeTimeID";
    names=names+" where Customer.CustomerID="+Convert.ToInt32(this.Customers_Name_List.SelectedValue)+" and Quotation.Version='"+this.VersionName.SelectedValue+"'";
    DataSet dsname=com.GetRecordset(names);
    if(dsname.Tables[0].Rows.Count>3)
    {
     application.Worksheets.Add(Type.Missing,Type.Missing,dsname.Tables[0].Rows.Count-3,Type.Missing);
    }
   
    for(int i=0;i<application.Worksheets.Count;i++)
    {
    
     Excel.Worksheet worksheet=(Excel.Worksheet)application.Worksheets.get_Item(i+1);
     
     if(i>dsname.Tables[0].Rows.Count-1)
     {
     
      worksheet.Delete();

     }
     else
     {
      worksheet.Activate();
     }
     string select="";
     if(dsname!=null)
     {
      if(i<dsname.Tables[0].Rows.Count)
      {
      
       worksheet.Name=dsname.Tables[0].Rows[i][0].ToString();
       //-------------------------
       select="select distinct Category_Name,Series_Name,Model_Name,Product_Name,Wattage,MaxWattage,Brand,Voltage,Description,Product_ESL,Plug,certificate,Product_Picture,Standard,LampBase,LifeTime,Quotation.MOQ,Quotation.Remark";
       string  itemname=common.single_datafield(this.Customers_Name_List.SelectedValue);
       select=select+itemname;
       select=select+" from Product";
       select=select+" inner join Categroy on Categroy.CategoryId=Product.CategroyID";
       select=select+" inner join Quotation on Product.ProductID=Quotation.ProductID";
       select=select+" inner join Customer on Customer.CustomerID=Quotation.CustomerID";
       select=select+" left outer join Wattage on Wattage.WattageID=Product.WattageID";
       select=select+" left outer  join MaxWattage on MaxWattage.MaxWattageID=Product.MaxWattageID";
       select=select+" left outer  join Brand on Brand.BrandID=Product.BrandID";
       select=select+" left outer  join Voltage on Voltage.VoltageID=Product.VoltageID";
       select=select+" left outer  join Product_ESL on Product_ESL.Product_ESLID=Product.Product_ESLID";
       select=select+" left outer  join Plug on Plug.PlugID=Product.PlugID";
       select=select+" left outer  join Certificate on Certificate.CertificateID=Product.CertificateID";
       select=select+" left outer  join Standard on Standard.StandardID=Product.StandardID";
       select=select+" left outer  join LampBase on LampBase.LampBaseID=Product.LampBaseID";
       select=select+" left outer  join LifeTime on LifeTime.LifeTimeID=Product.LifeTimeID";
       select=select+" where Customer.CustomerID="+Convert.ToInt32(this.Customers_Name_List.SelectedValue)+" and Quotation.Version='"+this.VersionName.SelectedValue+"'";
       select=select+" and Category_Name='"+dsname.Tables[0].Rows[i][0].ToString()+"'";
       System.Text.StringBuilder sb=new System.Text.StringBuilder();
       //Response.Write(select);
       if(i<1)
       {
        this.itemname();
       }
       DataSet ds=com.GetRecordset(select);
       int rowIndex=2; 
       int colIndex=1;
       int col=0;
       foreach(DataGridColumn col1 in this.InfoDataGrid.Columns) 
       { 
        col++; 
        application.Cells[1,col] = col1.HeaderText; 
        application.get_Range(application.Cells[1,col],application.Cells[1,col]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐 
        
       } 
       foreach(DataRow datarow in ds.Tables[0].Rows)
       {
        foreach(DataColumn datacolumn in ds.Tables[0].Columns)
        {
         if(colIndex==13)
         {
          //application.Cells[rowIndex,colIndex] ="ok";
          Excel.Pictures pictures=(Excel.Pictures)worksheet.Pictures(Type.Missing);
          Excel.Picture picture=pictures.Insert(@Server.MapPath(datarow[datacolumn].ToString()),Type.Missing);
          Excel.Range range=worksheet.get_Range("M"+rowIndex.ToString(),Type.Missing);
          picture.Left=(double)range.Left;
          picture.Top=(double)range.Top;
          picture.Width=(double)range.Width;
          picture.Height=(double)range.Height;
          
         }
         else
         {
          application.Cells[rowIndex,colIndex] =datarow[datacolumn].ToString(); 
         }
         
         colIndex=colIndex+1;
         if(colIndex>this.InfoDataGrid.Columns.Count)
         {
          colIndex=1;
         }
        }
        rowIndex=rowIndex+1;
       }
       //-----------------------
       if(i<1)
       {
        //break;
       }
      }
     }
     
     System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
     worksheet=null;
    }
   
    //Response.End();
    workbooks.get_Item(1).SaveCopyAs(Server.MapPath("Excel//Out1.xls"));
    workbooks.Close();
   
    application.DisplayAlerts=true;
    application.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
    workbooks=null;
    application=null;
    string path=Server.MapPath("Excel//Out1.xls");
    System.IO.FileInfo file = new System.IO.FileInfo(path); 
    Response.Clear(); 
    Response.Charset="GB2312"; 
    Response.ContentEncoding=System.Text.Encoding.UTF8; 
    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); 
    // 添加头信息,指定文件大小,让浏览器能够显示下载进度 
    Response.AddHeader("Content-Length", file.Length.ToString()); 
    
    // 指定返回的是一个不能被客户端读取的流,必须被下载 
    Response.ContentType = "application/ms-excel"; 
    
    // 把文件流发送到客户端 
    Response.WriteFile(file.FullName); 
    // 停止页面的执行 
   
    Response.End(); 
   }
  }

 

转载于:https://www.cnblogs.com/leonardleonard/archive/2007/03/21/1928197.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
工具: 【批量导入图片】 功能: 根据名称批量地导入图片 特点: 1.速度快:导入100图片只需要2秒钟 2.位置准:每张图片都导入到名称后面的单元格中,且刚好和单元格一样大,不会让多张图片重叠 3.大小灵活:导入的图片可以随心所欲定义其大小,且所有图片都占用相同的大小空间。但却保持图片导入前的比例,不会变形。 4.名称灵活:可以使用近似匹配。例如有两张名为“诺基亚8310”、“诺基亚7600”的图片,只需要在单元格输入“诺基亚”就行了,导入图片时可以将“诺基亚”开头的图片全部导入。 5.数量灵活:对于多个图片符合同一单元格名称的状况,可以将所有图片全部导入到其右边的单元格。 6.附件功能:图片导入到工作表后,将Excel默认的图片名“图片1”、“图片2”还原为图片本来的名称。 工具: 【隔行插入行】 功能: 对工作表隔行插入行,或者隔列插入列 特点: 1.可以每隔M行插入N行,其中N和M可以自己指定 2.可以每隔M列插入N列,其中N和M可以自己指定 3.起始行或结束行或者起始列、结束列也可以自己指定 工具: 【区域定位工具】 功能: 可以基于用户指定的条件选择相应的单元格,弥补Excel的不足 特点: 1.对于数值,可以使用所有比较运算符查找数据,Excel本身的定位不能定位于大于、小于某值的区域。 2.对于文本,可以定位于指定的字符,也可以定位于指定字符以外的所有字符。且支持通配符。 3.定位于可以指定区域,包括“选择区域”、“已用区域”和“可见区域”。这比Excel本身的查找更全面。 4.支持双条件查找,例如定位于从60到100的所有数据区域。或者不包括60到100的所有区域。 工具: 【根据工资计算钞票面额张数】 功能: 根据工资计算需要发工资时需要多少张100元、50元、20元、10元等等面额的钞票数量 特点: 1.可以选择一个区域进行操作,批量计算。 2.钞票的面额可以自己定义。 3.不仅计算每个人员需要的钞票张数,还对所有面额钞票汇总。 工具: 【建立工作表目录】 功能: 将当前工用簿中所有工作表建立目录,且产生链接 特点: 1.一键生成工作表目录。 2.单击目录中任意表名可以跳转至该工作表。 工具: 【建立文件目录】 功能: 将指定目录下所有文件建立目录于工作表中 特点: 1.可自由定义目标文件的路径。 2.允许选择目录是否产生链接。 3.可以对子目录中的文件建立目录,不管文件夹中有多少个子文件夹。 4.可以多次执行工具,从而将建立不同文件夹中的文件目录。程序自动将其置于不同的列中。 工具: 【工作簿拆】 功能: 将指定工作簿的每个工作表拆成单独的工作簿 特点: 1.自由选定存放路径。 2.提是拆后是否保留公式的选项。 3.可以自由定义参与拆的工作表名 工具: 【工作表拆】 功能: 将当前工作表的数据按条件拆成多个工作表 特点: 1.可以自由选择条件所有列。 2.可以设定标题行数,标题不参与拆。 3.可以自由定义折条件。 4.可以选择成多工作表后是否保留原有公式。 工具: 【合并工作簿】 功能: 将多个具有相同格式的工作簿中所有工作表合并为一个工作簿的多工作表中,也可以将一个工作簿的所有工作表合并到一个工作表 特点: 1.多工作簿合并时,可以自由指定待合并工作簿的目录路径。 2.可以选择将多工作簿合并到同工作表还是同工作簿。 3.对于多个工作簿中具有同名工作表时,可以选择是否合并工作表。 4.进以定义参与合并的工作簿名。即所选文件夹中存在多工作簿,可以对部执行合并。 5.如果仅对一个工作簿中的工作表进行合并,那么选择“多工作表合并”。 6.对工作表合并时,允许选择参与合并的工作表名。即既可全部参与合并也可以部工作表参与合并。 7.合并前需要将当前工作簿保存。 工具: 【选区背景着色】 功能: 将选择区域或者选区所在行、列用醒目的颜色标识,便于查看 特点: 1.着色的对象有“选区”、“行”、“列”以及“行列”可选。 2.可选颜色包括6种。 3.可以随时启用、禁用着色功能。 4.程序具有记忆功能。即保存上次的设置。 工具: 【小写转大写】 功能: 将阿拉伯数字转换成人民币大写形式 特点: 1.可以批量操作。 2.可以同时选择多个区域再执行工具。 3.几百个单元格数据瞬间完成转换。 工具: 【大写转小写】 功能: 将人民币大写转换成阿拉伯数字 特点: 1.可以批量操作。 2.可以同时选择多个区域再执行工具。 3.几百个单元格数据瞬间完成转换。 工具: 【批量新建或复制工作表】 功能: 批量新建或者工作表 特点: 1.操作灵活:可以选择新建或者复制。 2.批量操作:可以一次性建立或者复制1到255个。 3.高速操作:建立或者复制255个工作表三秒钟不到即可完成。 4.方向自由:可以定义新表在当前表之前或者之后。 5.命名灵活:对于表名有规律的创建或者复制新表,可以选择“从单元格创建”。 工具: 【批量加解密】 功能: 对当前工作簿中所有工作表或者指定的部工作表进行加、解密 特点: 1.可以灵活的指定加、解密工作表名。 2.对100个工作表加密或者解密不超过3秒钟。 3.本工个适用于多工作表使用相同密码保护,否则请手工操作。 4.其中解密是知道密码的前提下进行批量解密,而非绿色。 工具: 【生成工资条】 功能: 高效的工资条设计工具 特点: 1.瞬间将工资明细表生成工资条,方便打印并裁剪。 2.生成的工资与工资条之间有一个空行,供剪裁用。可以自定义其高度。 3.可以自己定义工资条头的行数,不同公司可能有不需要标题行。 4.只要类似工资条的信息都可以用本工具生成,例社保资料、成绩通知条等等。 工具: 【删除工资条】 功能: 删除前一工具生成的工资条头 特点: 1.可以由用户指定标题行数,使其适用面更广。 2.瞬间删除工资条标题信息,还原为明细表。 工具: 【反向选择】 功能: 对当前数据区域进行反向选择,即定位于未选择的区域 特点: 1.可以由用户指定标题行数,使其适用面更广。 2.瞬间删除工资条标题信息,还原为明细表。 备注:工具通用于Excel 2003和 EXCEL 2007 工具安装: 必须先解压,然后双击“安装.EXE" 如果需要删除工具,则双击“删除.EXE”

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值