NEWS

[C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window

[C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
Đăng bởi: Thảo Meo - Lượt xem: 2546 11:16:40, 06/07/2022C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách xuất tất cả hình ảnh từ File Excel ra ngoài thư mục windows trong lập trình c#, winform.

[C#] Save all image from file Excel (not support XLS) into folder windows

Các bạn thường thao tác import dữ liệu từ file excel vào cơ sở dữ liệu như: SQL server, mysql...

Nếu trong file excel đó có nhiều hình ảnh và các bạn muốn lấy những hình ảnh đó ra, thì bài viết này sẻ giúp các bạn.

export_image_from_excel

Ở hình trên, các bạn thấy file Excel: 

  1. Ô A: chứa thông tin mã nhân viên
  2. Ô B: chứa hình ảnh nhân viên

Đầu tiên, các bạn cài đặt cho mình thư viện CloseXML từ nuget.

PM> Install-Package ClosedXML -Version 0.96.0

Full source code c#:

using ClosedXML.Excel;
using ClosedXML.Excel.Drawings;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            var filename = @"Book1.xlsx";
            
            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                XLWorkbook workbook = new XLWorkbook(stream);
                var worksheet = workbook.Worksheet(1);               
               
                foreach (IXLPicture pic in worksheet.Pictures)
                {
                    var addressPicture = pic.TopLeftCell;
                    var image = Image.FromStream(pic.ImageStream);                    
                    var manv = worksheet.Cell(addressPicture.Address.RowNumber, "A" ).Value;
                    if (!Directory.Exists("images"))
                    {
                        Directory.CreateDirectory("images");
                    }
                    image.Save("images/" + manv + ".jpg", ImageFormat.Jpeg);

                }

            }

            MessageBox.Show("Done");
         
        }
    }
}

Thanks for waching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
Đăng bởi: Thảo Meo - Lượt xem: 2546 11:16:40, 06/07/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.