Insert images while importing data to the EXECL file, and insert images to execl
Because the project needs to export data to the EXECL document while exporting images, some problems encountered during processing are recorded here.
When the code is written and put on the test server for execution, an error is reported, indicating that the component whose CLSID is {00024500-0000-0000-C000-000000000046} in the COM class factory fails to be retrieved because the following error occurs: 80070005.
This solution is basically implemented on the Internet, but note that,After setting permissions, you must assign permissions to the office folder installed on the server.
After the permission questions are set up, the debugging code is completed. All the information found on the Internet is to insert data first, and then insert the image at the end of the data, instead of having an image for each row I want, so it does not meet my needs, so let's look at the code on the Internet.
/// <Summary> //// </summary> /// <param name = "dt"> data source </param> /// <param name =" filepath "> file path strength, for example, (E: \ Execl \ 201706070001.xls) </param> protected void ExportExcel (DataTable dt, string filepath) {if (dt = null | dt. rows. count = 0) return; Microsoft. office. interop. excel. application xlApp = new Microsoft. office. interop. excel. application (); object m_objOpt = System. reflection. missing. value; if (xlApp = null) {return;} // System. globalization. cultureInfo CurrentCI = System. threading. thread. currentThread. currentCulture; System. threading. thread. currentThread. currentCulture = new System. globalization. cultureInfo ("en-US"); Microsoft. office. interop. excel. workbooks workbooks = xlApp. workbooks; Microsoft. office. interop. excel. workbook workbook = workbooks. add (Microsoft. office. interop. excel. xlWBATemplate. xlWBATWorksheet); Microsoft. office. interop. excel. worksheet worksheet = (Microsoft. office. interop. excel. worksheet) workbook. worksheets [1]; Microsoft. office. interop. excel. range range; long totalCount = dt. rows. count; long rowRead = 0; for (int I = 0; I <dt. columns. count; I ++) {worksheet. cells [1, I + 1] = dt. columns [I]. columnName; range = (Microsoft. office. interop. excel. range) worksheet. cells [1, I + 1]; range. interior. colorIndex = 15 ;}for (int r = 0; r <dt. rows. count; r ++) {for (int I = 0; I <dt. columns. count; I ++) {try {if (I = 4) {range = worksheet. get_Range ("E" + (r + 2), m_objOpt); // This is because I want to insert an image to column E of the document, so I added a judgment, and the range of column E is written. select (); float PicLeft, PicTop; PicLeft = Convert. toSingle (range. left); PicTop = Convert. toSingle (range. top); range. columnWidth = 60; // set the wide range of cells. rowHeight = 120; // set the height of the cell worksheet. shapes. addPicture (dt. rows [r] [I]. toString (), Microsoft. office. core. msoTriState. msoFalse, Microsoft. office. core. msoTriState. msoCTrue, PicLeft, PicTop, 100,100); // The following 100 sets the width and height of the image} else worksheet. cells [r + 2, I + 1] = dt. rows [r] [I]. toString ();} catch {worksheet. cells [r + 2, I + 1] = dt. rows [r] [I]. toString (). replace ("=", "") ;}} rowRead ++;} xlApp. visible = true; workbook. saved = true; workbook. saveAs (filepath); workbook. close (true, Type. missing, Type. missing); workbook = null; xlApp. quit (); xlApp = null ;}