c# winform excel导入数据出现4.0或12.0未注册的问题

//打开文件窗体选择excel文件
public static string Open(string filename)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = “Excel(.xlsx)|.xlsx|All File(.)|.”;
dialog.Title = “打开”;
dialog.RestoreDirectory = true;
dialog.FileName = filename;
string result;
if (dialog.ShowDialog() == DialogResult.OK)
{
result = dialog.FileName;
}
else
{
result = string.Empty;
}
return result;
}

//拿到excel里面的数据,如果有图片就加上第三个方法(InsertPicturesIntoDataTable)
public static bool ExcelFileToDataTable(string filepath, out System.Data.DataTable datatable, bool exportColumnName, out string error)
{
error = “”;
datatable = null;
bool result;
try
{
if (!System.IO.File.Exists(filepath))
{
error = “文件不存在”;
datatable = null;
result = false;
}
else
{
Workbook workbook = new Workbook(filepath);
Worksheet worksheet = workbook.Worksheets[0];
datatable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, exportColumnName);

Aspose.Cells.Drawing.PictureCollection pictures = worksheet.Pictures;
if (pictures.Count > 0)
{
string error2 = “”;
if (InsertPicturesIntoDataTable(pictures, datatable, out datatable, out error2) == false)
{
error = error + error2;
}
}

datatable.TableName = worksheet.Name;
result = true;
}
}
catch (System.Exception e)
{
error = e.Message;
result = false;
}
return result;
}

//把图片插入到相应的gridcontrol的表格中
private static bool InsertPicturesIntoDataTable(Aspose.Cells.Drawing.PictureCollection pictures, DataTable fromdatatable, out DataTable datatable, out string error)
{
error = “”;
datatable = fromdatatable;
datatable.Columns.Add(“orImg”,typeof(Image));
//把图片按位置插入Table中
DataRow[] rows = datatable.Select();
foreach (Aspose.Cells.Drawing.Picture picture in pictures)
{
try
{
System.Console.WriteLine(picture.GetType().ToString());

//----把图片转换成System.Drawing.Image----
MemoryStream mstream = new MemoryStream();
mstream.Write(picture.Data, 0, picture.Data.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(mstream);
//----Image放入DataTable------
//datatable.Columns[picture.UpperLeftColumn].DataType = image.GetType();
rows[picture.UpperLeftRow-1][“orImg”] = image;
}
catch (System.Exception e)
{
error = error + " InsertPicturesIntoDataTable: " + e.Message;
}
}

return true;
}

//** 注意: 项目引用一个插件Aspose.Cells的插件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值