把一张图片画在excel表格中

之前闲得没事干,了解了C#中有关Bitmap的简单使用。

尝试写了一个将图片画在excel表格中的一个程序

逻辑就是将图片通过Bitmap.GetPixel(x, y)获取指定像素位置的颜色,并将其填充在excel单元格内

由于我使用的是xls,2003版最大行数是65536行,最大列数是256列

所以在处理图片时,受到excel最大列数的限制,所以需要将图片进行等比例缩放,使其宽度保持在256px及一下,

这里使用了Bitmap(Bitmap, new Size(width, height))进行图片的缩放。

补充:进行excel表格的绘制时使用了插件Aspose.Cells。

下面为效果图:

把一张图片画在excel表格中

 

下面的部分代码


using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace imgtoexcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        public void forColor(string fileName)
        {
            initExcel();
            Cells cells = sheet.Cells;
            Bitmap bm = new Bitmap(fileName);

            if (bm.Width > 256)
            {
                //计算 最大列数是256 
                Decimal wx = (Decimal)256 / (Decimal)bm.Width;
                int newheight = Convert.ToInt32(wx * bm.Height);
                Bitmap nbm = new Bitmap(bm, new Size(256, newheight));//缩放
                bm = nbm;
            }
            for (int x = 0; x < 256; x++)
            {
                sheet.Cells.SetColumnWidth(x, 1);
                
                for (int y = 0; y < bm.Height; y++)
                {
                    sheet.Cells.SetRowHeight(y, 10);
                    Color pixelColor = bm.GetPixel(x, y);
                    byte[] rgb = new byte[] { pixelColor.R, pixelColor.G, pixelColor.B };
                    cells[y, x].Value = "";
                    Style s = cells[y, x].GetStyle();
                    s.ForegroundColor = Color.FromArgb(pixelColor.A, pixelColor.R, pixelColor.G, pixelColor.B);
                    s.Pattern = BackgroundType.Solid;
                    cells[y, x].SetStyle(s);
                }
            }

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel(*.xls)|*.xls";
            sfd.FileName = "保存";
            sfd.DefaultExt = "xls";
            sfd.AddExtension = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                workbook.Save(sfd.FileName);
            }
        }
        public static Workbook workbook { get; set; }
        public static Worksheet sheet { get; set; }
        public void initExcel()
        {
            workbook = new Workbook();
            sheet = workbook.Worksheets[0];

        }


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = @"C:\";
            openFileDialog.Filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Thread.Sleep(1000);
                forColor(openFileDialog.FileName);
            }
        }

    }
}

 

上一篇:Facebook跨境电商广告投放入门篇


下一篇:Android将Bitmap缩放到指定大小,并可兼容特殊机型,如三星、一加手机等