How do I import data and insert pictures in the Execl file at the same time?

Source: Internet
Author: User
Because the project needs to export the data to the EXECL document at the same time also need to export the picture in, in the processing is encountered some problems, here to record.

First the code is written, then put the test server to execute the error, reported that the COM class factory in the CLSID {00024500-0000-0000-c000-000000000046} component failed because of the following error: 80070005.

This workaround is basically based on the internet, but it is important to note that after you set up permissions, you also need to assign permissions to the Office folders installed on the server.

Permissions problem set up after debugging code, the online search for data are inserted first, and then at the end of the data inserted in the picture, instead of every line I want to have pictures, so do not meet my needs, so I look at the online code of the whole bar.

<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 a picture into the e column of the document, so I added a judgment and wrote the e column range.                            Select (); float picleft, pictop; Picleft = Convert.tosingle (range.                            left); Pictop = Convert.tosingle (range.                            TOP); Range. ColumnWidth = 60;//Sets the width range of the cell. RowHeight = 120;//sets the high worksheet of the cell. Shapes.addpicture (dt. Rows[r][i]. ToString (), Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Picleft, Pictop, 100, 100);//The 100 behind this is the width height}elseworksheet of the set picture. 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; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.