在裸机中运行导出Excel出错,提示"检索com类工厂中CLSID为{略}的组件时失败,原因出现以下错误:80040154(略)",并且在其逻辑系统中运行打包进去的Excel.exe也是不行,请问有什么办法可以解决它呢??可以给出具体点的解决方案吗??

解决方案 »

  1.   

    直接写EXCEL文件,网上有代码。
    另外,就算是你安装了EXCEL,也不能保证在数据量非常大的情况下不出问题。所以直接写EXCEL文件是根本解决之道。
      

  2.   

                if (DGV.RowCount == 0)
                {
                    MessageBox.Show("没有数据可供导出", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                EnableBtn();
                string filepath = "";
                int excelpos = 0;
                sFDExcel.Filter = "Excel表格|*.xls";
                sFDExcel.ShowDialog();
                if (sFDExcel.FileName.Equals(""))
                {
                    return;
                }
                else
                {
                    filepath = sFDExcel.FileName;
                    if (File.Exists(filepath))
                    {
                        File.Delete(filepath);
                    }
                }
                object miss = Type.Missing;
                Excel.Application exap = new Excel.Application();
                Excel.Workbook workbook = exap.Workbooks.Add(true);
                Excel.Sheets shts = workbook.Sheets;
                try
                {
                    Excel.Worksheet sht = (Excel.Worksheet)shts.Add(miss, workbook.Sheets[workbook.Sheets.Count], 1, miss);
                    sht.Name = "库存信息";
                    ((Excel.Range)sht.Cells[1, 3]).Value2 = "类型";
                    ((Excel.Range)sht.Cells[1, 1]).Value2 = "编号";
                    ((Excel.Range)sht.Cells[1, 2]).Value2 = "名称";
                    ((Excel.Range)sht.Cells[1, 4]).Value2 = "数量";
                    ((Excel.Range)sht.Cells[1, 5]).Value2 = "售价";
                    ((Excel.Range)sht.Cells[1, 6]).Value2 = "备注";
                    excelpos = 1;
                    foreach (DataGridViewRow dr in DGV.Rows)
                    {
                        excelpos = excelpos + 1;
                        ((Excel.Range)sht.Cells[excelpos, 3]).Value2 = dr.Cells["ClsName"].Value.ToString();
                        ((Excel.Range)sht.Cells[excelpos, 1]).Value2 = dr.Cells["Barcode"].Value.ToString();
                        ((Excel.Range)sht.Cells[excelpos, 2]).Value2 = dr.Cells["GodName"].Value.ToString();
                        ((Excel.Range)sht.Cells[excelpos, 4]).Value2 = dr.Cells["NowAmt"].Value.ToString();
                        ((Excel.Range)sht.Cells[excelpos, 5]).Value2 = dr.Cells["SelPrice"].Value.ToString();
                        ((Excel.Range)sht.Cells[excelpos, 6]).Value2 = dr.Cells["StcNote"].Value.ToString();
                        System.Windows.Forms.Application.DoEvents();
                    }
                    MessageBox.Show("你已经成功导出Excel", " 提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, " 提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    workbook.SaveCopyAs(filepath);
                    workbook.Saved = true;
                    //workbook.Save();
                    //exap.Save(filepath);
                    exap.Quit();
                    EnableBtn();
                }
    不是这样写到Excel文件吗??
      

  3.   

    如果是获取数据源后绑定到dgv的也可以直接将数据源导入excel 参考
    http://topic.csdn.net/u/20071129/15/9d1878af-89a8-441a-8681-88a5b46e684f.html
      

  4.   

    循环写进EXCEL方便也便于控制。
    代码我BLOG里有相关资料。