带有图像的 ClosedXML 导出数据表

ClosedXML export datatable with image

我正在尝试使用 ClosedXML 将数据从数据库导出到 Excel。 datatable里面有一张id图片,我只试过导出数据文本,想知道如何把学生的图片包含到Excel。

public void ExportToExcel()
{
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel files|*.xlsx";
        sfd.Title = "Save an Excel File";
        sfd.FileName = "Student List";

        cn.Open();

        cm = new SqlCommand("SELECT s.Lname, s.Fname, s.Mname, s.Gender, s.MobileNum, c.Course, s.Scholarship, s.EmailAddress, s.Address, s.District, s.ZipCode, s.Birthdate, " +
            "s.Birthplace, s.Citizenship, s.MotherName, s.MotherContact, s.FatherName, s.FatherContact, s.ZoomAcc, s.FbAcc, s.EducAtt, s.EmploymentStat, s.AssessmentResult, s.ProfilePic" +
            "FROM Student s INNER JOIN Course c ON s.CourseID = c.CourseID WHERE s.StudentID = @id", cn);
        cm.Parameters.AddWithValue("@id", lblID);

        SqlDataAdapter da = new SqlDataAdapter(cm);

        DataTable dt = new DataTable();
        da.Fill(dt);

        cn.Close();

        using (XLWorkbook wb = new XLWorkbook())
        {
            var ws = wb.Worksheets.Add(dt, "Student List");
            ws.Columns().AdjustToContents();

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                wb.SaveAs(sfd.FileName);
                this.Cursor = Cursors.WaitCursor;
                MyMessageBox.ShowMessage("Data successfully backed up!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Cursor = Cursors.Default;
                wb.Dispose();
            }
        }
}

有谁知道在什么地方添加导出图片的代码?

实际上 Image SQL 数据类型有点过时,但会在 c# 中转换为 byte[],因此这个或稍加修改的代码就可以解决问题

    using (XLWorkbook wb = new XLWorkbook())
    {
        var ws = wb.Worksheets.Add(dt, "Student List");
        ws.Columns().AdjustToContents();

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            int numRow = 1;
            foreach (DataRow row in dt.Rows)
            {
                    ws.Cell(numRow, 1).Value = (string)row["Lname"];
                    ws.Cell(numRow, 2).Value = (string)row["Fname"];
                    ws.Cell(numRow, 3).Value = (string)row["Mname"];
                    ws.Cell(numRow, 4).Value = (string)row["Gender"];
                    ws.Cell(numRow, 5).Value = (double)row["MobileNum"];
                    //[...] do with all the needed cells
                    var image = ws.AddPicture(new MemoryStream((byte[])row["ProfilePic"])) //the cast is only to be sure
                                        .MoveTo(ws.Cell(numRow, 7)) //Or the cell you want to bind the picture
                                        .Scale(0.5);
                    numRow++;
            }
            wb.SaveAs(sfd.FileName);
            this.Cursor = Cursors.WaitCursor;
            MyMessageBox.ShowMessage("Data successfully backed up!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Cursor = Cursors.Default;
            wb.Dispose();
        }
    }

实际上,您可以添加数据表,只需将其添加为工作表

// Add a DataTable as a worksheet
wb.Worksheets.Add(dataTable);

但是这个不适用于图像,所以我使用了一个 for 循环来完成所有工作(并教授如何访问单元格的值)