gpt4 book ai didi

c# - 使用适当的表格格式将带有图像的 DataGridView 数据导出到 Excel、HTML 或 Word

转载 作者:行者123 更新时间:2023-11-30 22:33:25 25 4
gpt4 key购买 nike

我有一个数据 GridView ,其中加载了图像。作为此数据网格源的表具有图像路径,我使用该路径加载图像。我尝试将数据导出到 Excel 并成功了,但它似乎显示了一些图像问题。请任何帮助?任何帮助代替 Excel、HTML 或 Word 或任何东西都可以,但请提供详细的帮助,否则会导致很多问题。

这是我用来导出到 Excel 的代码:

saveFileDialog1.Filter = "Excel (*.xls)|*.xls";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (!saveFileDialog1.FileName.Equals(String.Empty))
{
FileInfo f = new FileInfo(saveFileDialog1.FileName);
if (f.Extension.Equals(".xls"))
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;

for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{

DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet.Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
xlWorkSheet.Columns.AutoFit();
if (cell.Value.GetType() == typeof(Bitmap))
{
xlWorkSheet.Cells[i + 1, j + 1] = ReadFile((Bitmap)cell.Value);
}
else
{
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}

}
}

xlWorkBook.SaveAs(saveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

MessageBox.Show("Excel file created , you can find the file " + saveFileDialog1.FileName);
}
else
{
MessageBox.Show("Invalid file type");
}
}
else
{
MessageBox.Show("You did pick a location " +
"to save file to");
}
}

表格格式为:

斧头号rm_no全名类型照片//文件路径。指纹//文件路径。

都是字符串。我也尝试过使用 Spire.dataExport 和 iTextSharp,但它不起作用。

最佳答案

如果您需要将图像放入单元格中,请尝试以下代码:

if (cell.Value.GetType() == typeof(Bitmap))
{
// You have to get original bitmap path here
string imagString = "bitmap1.bmp";
Excel.Range oRange = (Excel.Range)xlWorkSheet.Cells[i + 1, j + 1];
float Left =(float) ((double)oRange.Left);
float Top =(float) ((double)oRange.Top);
const float ImageSize=32;
xlWorkSheet1.Shapes.AddPicture(imagString, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
oRange.RowHeight = ImageSize + 2;
}
else
{
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}

关于c# - 使用适当的表格格式将带有图像的 DataGridView 数据导出到 Excel、HTML 或 Word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8312257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com