使用Aspose.Cells导出EXCEL且导入图片可自动缩小

来源:互联网 发布:javascript艺术编程pdf 编辑:程序博客网 时间:2024/04/29 11:35

下面我们先导出一个可以自动缩小图片的EXCEL代码如下:

1.先引用Aspose.Cells参考,下载地址:http://download.csdn.net/detail/wubing1111/7268739

2.引用命名空间:using Aspose.Cells;
        using System.Drawing;
        using System.IO;

  public void ImportNoTemp()
        {
            Workbook workbook = new Workbook();

            Worksheet sheet = (Worksheet)workbook.Worksheets[0];
            Cells cells = sheet.Cells;//单元格
            for (int i = 1; i < 4; i++)
            {

                sheet.Cells["A" + i.ToString()].PutValue("TEST");

                sheet.Cells["B" + i.ToString()].PutValue("日期");
                byte[] bytes = null;
                if (i == 1)
                {
                    bytes = SmallImageDatabytes(@"D:\PIC\t1.jpg", 60, 40);//自動縮小圖片後轉換為二進制流
                }
                else if (i == 2)
                {
                    bytes = SmallImageDatabytes(@"D:\PIC\t2.jpg", 60, 40);//自動縮小圖片後轉換為二進制流
                }
                else if (i == 3)
                {
                    bytes = SmallImageDatabytes(@"D:\PIC\t3.jpg", 60, 40);//自動縮小圖片後轉換為二進制流
                }
                MemoryStream ms = new MemoryStream(bytes);

                System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
                cells.SetRowHeight(i - 1, returnImage.Height); //設定為圖片高度
                sheet.Pictures.Add(i - 1, 3, ms);//導入圖片到EXCEL
                //


            }
            string filename = string.Format("{0}{1}.xls", "saleranklist", Convert.ToDateTime(DateTime.Now).ToString("yyyyMMdd")); //文件默认命名方式,可以自定义
            //寫法一

            Response.ContentType = "application/ms-excel;charset=utf-8";
            Response.AddHeader("content-disposition", "attachment; filename=" + filename);
            MemoryStream memStream = workbook.SaveToStream();
            Response.BinaryWrite(memStream.ToArray());
            Response.End();

            //寫法二
            //Response.Clear();
            //Response.Buffer = true;
            //Response.Charset = "utf-8";
            //Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
            //Response.ContentEncoding = System.Text.Encoding.UTF8;
            //Response.ContentType = "application/ms-excel";
            //Response.BinaryWrite(workbook.SaveToStream().ToArray());
            //Response.End(); 
        }

/// <summary>
        /// 按給寬與長自動縮小圖片後轉換為二進制流
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="intWidth"></param>
        /// <param name="intHeight"></param>
        /// <returns></returns>
        public static byte[] SmallImageDatabytes(string FilePath, int intWidth, int intHeight)
        {
            if (!File.Exists(FilePath))
                return null;

            Bitmap objPic = new System.Drawing.Bitmap(FilePath);

            Bitmap myBitmap = new System.Drawing.Bitmap(objPic, intWidth, intHeight);

            using (MemoryStream curImageStream = new MemoryStream())
            {
                myBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png);
                curImageStream.Flush();

                byte[] bmpBytes = curImageStream.ToArray();
                //如果转字符串的话
                //string BmpStr = Convert.ToBase64String(bmpBytes);
                return bmpBytes;
            }
        }

 

0 0