Heim  >  Artikel  >  Backend-Entwicklung  >  Wie kann ich gleichzeitig Daten importieren und Bilder in eine EXECL-Datei einfügen?

Wie kann ich gleichzeitig Daten importieren und Bilder in eine EXECL-Datei einfügen?

零下一度
零下一度Original
2017-06-23 16:21:401439Durchsuche

因为项目需要在导出数据到EXECL文档的同时还需要导出图片进去,在处理是遇到的一些问题,在此记录一下。

首先代码写好之后放测试服务器上去执行的时候报错了,报检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。

这个解决方法我基本上是按照网上来进行解决的,但是需要注意的就是,设置完权限之后,还需要在服务器安装的office文件夹分配权限。

权限问题设置完之后就是调试代码了,网上搜索到的资料都是先插入数据,然后在数据的最后插入图片,而不是我想要的每一行都有图片,所以不符合我的需求,于是就自己看着网上的代码整吧整吧。

/// <summary>/// /// </summary>/// <param name="dt">数据源</param>/// <param name="filepath">文件路劲例如(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); //这里是因为我要给文档的E列插入图片,所以加了判断,并写死了E列
                            range.Select();float PicLeft, PicTop;
                            PicLeft = Convert.ToSingle(range.Left);
                            PicTop = Convert.ToSingle(range.Top);

                            range.ColumnWidth = 60;//设置单元格的宽
                            range.RowHeight = 120;//设置单元格的高worksheet.Shapes.AddPicture(dt.Rows[r][i].ToString(), Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, PicLeft, PicTop, 100, 100);//这后面的100 是设置图片的宽高
                        }elseworksheet.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;
        }

 

Das obige ist der detaillierte Inhalt vonWie kann ich gleichzeitig Daten importieren und Bilder in eine EXECL-Datei einfügen?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn