如何将dataGridViewImageColumn中的图片导出到excel中

量化相对论 2016-09-12 09:56:36
建了一个dataGridView,其中第5,6列是图片

DataGridViewRow row = new DataGridViewRow();
int index = dataGridView1.Rows.Add(row);
dataGridView1.Rows[index].Cells[0].Value = folderTime;
dataGridView1.Rows[index].Cells[1].Value = strName;
dataGridView1.Rows[index].Cells[2].Value = strID;
dataGridView1.Rows[index].Cells[3].Value = strGender;
dataGridView1.Rows[index].Cells[4].Value = Image.FromFile(imagePath1);
dataGridView1.Rows[index].Cells[5].Value = Image.FromFile(imagePath2);




我想把所有的信息导出到excel中

int rowNum = dataGridView1.Rows.Count; //行数
int columnNum = dataGridView1.Columns.Count; //列数
int rowIndex = 0;
int columnIndex = 0;

if (rowNum != 0)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
if (xlApp == null)
{
MessageBox.Show("无法创建excel对象");
return;
}
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);

for (columnIndex = 0; columnIndex < columnNum; columnIndex++)
{
xlApp.Cells[1, columnIndex + 1] = dataGridView1.Columns[columnIndex].HeaderText;
}


for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex + 1, columnIndex] = dataGridView1[j, i].Value + "\t";

}
}
xlBook.SaveCopyAs(strFileName);
MessageBox.Show("OK");

数据导出没问题,图片不能显示
...全文
472 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
SJYnight 2018-08-19
  • 打赏
  • 举报
回复
试试这个

/// <summary>
/// 将图片保存至EXCEL
/// </summary>
/// <param name="rg">图片存放到EXCEL中的RG</param>
/// <param name="worksheet"></param>
/// <param name="img"> 图片 </param>
private void insertPict(Range rg, _Worksheet worksheet, string img)
{
rg.Select();
Excel.Pictures pics = (Excel.Pictures)worksheet.Pictures(Type.Missing);
pics.Insert(img, Type.Missing);
}

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧