将具有“图像类型”列的SQL数据表导出到Excel,然后再次导入

时间:2018-07-09 07:24:59

标签: c# sql-server-2008

我有一个表Employee,其图像类型为cloumn,用于存储每个Employee的图像。我可以毫无问题地以Windows形式存储和检索此Image列到Picturebox。但是,当我使用Microsoft Interop将该表导出和导入到Excel文件中时,如果没有“图像”列类型,则效果很好。 这是将SQL DataTable导出到Excel文件的代码。

private void ExportToExel()
{
    string data = null;
            var i = 0;
            var j = 0;
            var xlApp = new Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
            var ds = new DataSet();
            ds = hoso_nvExport();        
            foreach (DataTable dt in ds.Tables)
            {
                for (var i1 = 0; i1 < dt.Columns.Count; i1++)
                {
                    xlWorkSheet.Cells[1, i1 + 1] = dt.Columns[i1].ColumnName;
                }
            }
            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                var s = i + 1;
                var _totalProgress = ds.Tables[0].Rows.Count - 1;
                for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    data = ds.Tables[0].Rows[i].ItemArray[j].ToString();                   
                    xlWorkSheet.Cells[s + 1, j + 1].EntireColumn.AutoFit();
                    xlWorkSheet.Cells[s + 1, j + 1] = data;
                }
            }
            xlWorkBook.SaveAs(savePath);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
}

这是将数据再次从Excel文件导入SQL表的代码。

public void ImportDataFromExcel(string excelFilePath)
        {     
            var ssqltable = "[HRMVN].[dbo].[hoso_nv]";         
            var myexceldataquery = "select * from [Sheet1$]";
                var sexcelconnectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath +
                                             ";Extended Properties='Excel 8.0;HDR={1}'";
                using (OleDbConnection connection =
                    new OleDbConnection(sexcelconnectionstring))
                {
                    OleDbCommand command = new OleDbCommand
                        ("Select * FROM [Sheet1$]", connection);
                    connection.Open(); 
                    using (DbDataReader dr = command.ExecuteReader())
                    {
                        var sqlconn = new SqlConnection(strConStr);
                        sqlconn.Open();
                        using (SqlBulkCopy bulkCopy =new SqlBulkCopy(sqlconn))
                        {
                            bulkCopy.DestinationTableName = ssqltable;
                            bulkCopy.WriteToServer(dr);
                            dr.Close();
                            connection.Close();
                            sqlconn.Close();
                            MessageBox.Show("File has been imported into SQL Server.");
                        }
                    }
                }                
            }           
        }

导出到Excel文件时,“图像类型”列仅包含System.Byte[] 因此,当我再次将其导入到SQL表中时。我收到错误消息:

  

无效的将Sytem.String强制转换为System.Byte []

“图像类型”列应为:

0x89504E470D0A1A0A0000000D494844520000012A0000012A080600

导出到Excel时仅System.Byte[]

因此,请帮助我将“图像类型”列(二进制数据)导出为字符串或任何类型,以便我可以通过C#将其再次导入到SQL表中。

0 个答案:

没有答案