private static bool InsertPicturesIntoDataTable(Aspose.Cells.Pictures pictures, DataTable fromdatatable, out DataTable datatable, out string error) { error = ""; datatable = fromdatatable; //把图片按位置插入Table中 DataRow[] rows = datatable.Select(); foreach (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][picture.UpperLeftColumn] = image; } catch (System.Exception e) { error = error + " InsertPicturesIntoDataTable: " + e.Message; } } return(true); }
/// <summary> /// Excel文件转换为DataTable. /// </summary> /// <param name="filepath">Excel文件的全路径</param> /// <param name="datatable">DataTable:返回值</param> /// <param name="error">错误信息:返回错误信息,没有错误返回""</param> /// <returns>true:函数正确执行 false:函数执行错误</returns> public static bool ExcelFileToDataTable(string filepath, out DataTable datatable, out string error) { error = ""; datatable = null; try { if (File.Exists(filepath) == false) { error = "文件不存在"; datatable = null; return(false); } Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(); workbook.Open(filepath); Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0]; datatable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, true); //-------------图片处理------------- Aspose.Cells.Pictures pictures = worksheet.Pictures; if (pictures.Count > 0) { string error2 = ""; if (InsertPicturesIntoDataTable(pictures, datatable, out datatable, out error2) == false) { error = error + error2; } } return(true); } catch (System.Exception e) { error = e.Message; return(false); } }