C# 使用OleDbConnection在c中导出excel文档中的图像时出现的问题#
我想用c语言将图像导出到excel文档# 该图像包含以下格式: System.Windows.Media.Imaging.BitmapImage 以下是连接详细信息:C# 使用OleDbConnection在c中导出excel文档中的图像时出现的问题#,c#,image,excel,export,oledbcommand,C#,Image,Excel,Export,Oledbcommand,我想用c语言将图像导出到excel文档# 该图像包含以下格式: System.Windows.Media.Imaging.BitmapImage 以下是连接详细信息: private const string OleDbConnection = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=Excel 8.0;"; private const string OleDbInsert = "INSERT
private const string OleDbConnection = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties=Excel 8.0;";
private const string OleDbInsert = "INSERT INTO [Report] ([Time], [Elapsed], [Description], [Picture]) values( @timestamp, @elapsed, @description, @pic);";
private const string OleDbCreate = "CREATE TABLE [Report] ([Time] varchar(255), [Elapsed] varchar(255), [Description] varchar(255), [pic] blob);";
我使用以下代码在excel中编写图像:
foreach (var e in Entries)
{
using(var command = new OleDbCommand { Connection = connection, CommandText = OleDbInsert })
{
command.Parameters.Add(new OleDbParameter("@component", OleDbType.BSTR) { Value = e.val1 });
command.Parameters.Add(new OleDbParameter("@result", OleDbType.BSTR) { Value = e.val2 });
command.Parameters.Add(new OleDbParameter("@description", OleDbType.BSTR) { Value = e.val3});
if (e.EncodedScreenshot != string.Empty)
{
byte[] imageContent = imageToByteArray(ImageWpfToGDI(e.Picture));
OleDbParameter ph = new OleDbParameter("@pic", OleDbType.Binary) { Value = imageContent};
ph.Size = imageContent.Length;
command.Parameters.Add(ph);
}
}
以下是我正在使用的方法:
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
private System.Drawing.Image ImageWpfToGDI(System.Windows.Media.Imaging.BitmapImage image)
{
MemoryStream ms = new MemoryStream();
var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(image as System.Windows.Media.Imaging.BitmapSource));
encoder.Save(ms);
ms.Flush();
return System.Drawing.Image.FromStream(ms);
}
除图像外,所有数据都会导出到Excel文件(并且肯定存在图像)。你能给我一些关于如何解决这个问题的建议吗?我一直在网上搜索,但我仍然找不到解决方案
我根据此链接进行了一些修改:。我相信我的错误是因为图像数据类型。我应该为DB和OLEDB参数中的图像使用什么数据类型
有人能帮帮我吗?如蒙答复,我将不胜感激
非常感谢!:) 以防万一有人碰到同样的问题 我找不到使用OLEDBPParameter将图像导出到excel文件的方法。如果有人知道怎么做,请纠正我 相反,我使用了这种方法: 借助此帮助: 它工作得非常好