ClosedXML 导出带有图像的数据表
我正在尝试使用 ClosedXML 将数据从数据库导出到 Excel。数据表中有一张身份证图片,我只尝试导出数据文本,我想知道如何将学生的图片包含到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();
}
}
}
有谁知道在哪里添加导出图像的代码?
I am trying to export data from database to Excel using ClosedXML. There is an id picture in the datatable, I have only tried exporting data texts and I want to know how to include the of picture of the student to 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();
}
}
}
Does anyone know what and where to add the code for exporting image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,Image SQL 数据类型有点老式,但会在 C# 中转换为 byte[],因此这个稍微修改一下的代码就可以解决问题。
实际上,您可以添加一个数据表,只需将其添加为工作表,
但这没有'无法使用图像,因此我使用 for 循环来完成所有工作(并教授如何访问单元格的值)
Actually Image SQL datatype is a little bit old fashioned, but will translate to byte[] in c#, so this o r a slightly modified code will do the trick
Actually, you can Add a datatable simply adding it as worksheet
but this didn't work with Images, so I used a for loop to do all work (and teach how to access Cell's Values)