简体   繁体   English

内存不足,无法创建Excel工作簿

[英]insufficient memory to create an excel workbook

My original code: 我的原始代码:

    Excel.Application xlErrorApp;
    Excel.Workbook xlErrorWorkBook;
    Excel.Worksheet xlErrorWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    xlErrorApp = new Excel.Application();
    xlErrorWorkBook = xlErrorApp.Workbooks.Add(); // -> error
    xlErrorWorkSheet = (Excel.Worksheet)xlErrorWorkBook.Worksheets.get_Item(1);

A get error in line 6. 第6行出现get错误。

Error Message: 错误信息:

Application Microsoft Excel can not open or save documents due to insufficient memory or disk space. 由于内存或磁盘空间不足,应用程序Microsoft Excel无法打开或保存文档。

Which library are you using ? 您正在使用哪个库?

I already said it but actually Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. 我已经说过了,但实际上Microsoft目前不建议也不支持任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office应用程序自动化。在此环境中运行Office时,可能会表现出不稳定的行为和/或死锁。

I recommend you to look for a free library like Open Office XML or a not free library like Aspose. 我建议您寻找像Open Office XML这样的免费库,或者像Aspose这样的免费库。

how about this: 这个怎么样:

Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing);

Workbooks.Add() method from MSDN ; 来自MSDN的 Workbooks.Add()方法;

Parameters
Template
Type: System.Object

Optional Object. 可选对象。 Determines how the new workbook is created. 确定如何创建新工作簿。 If this argument is a string specifying the name of an existing Microsoft Excel file, the new workbook is created with the specified file as a template. 如果此参数是指定现有Microsoft Excel文件名称的字符串,则会使用指定文件作为模板来创建新工作簿。 If this argument is a constant, the new workbook contains a single sheet of the specified type. 如果此参数为常量,则新工作簿将包含指定类型的单个工作表。 Can be one of the following XlWBATemplate constants: xlWBATChart, xlWBATExcel4IntlMacroSheet, xlWBATExcel4MacroSheet, or xlWBATWorksheet. 可以是以下XlWBATemplate常数之一:xlWBATChart,xlWBATExcel4IntlMacroSheet,xlWBATExcel4MacroSheet或xlWBATWorksheet。 If this argument is omitted, Microsoft Excel creates a new workbook with a number of blank sheets (the number of sheets is set by the SheetsInNewWorkbook property). 如果省略此参数,Microsoft Excel将创建一个具有许多空白工作表的新工作簿(工作表数由SheetsInNewWorkbook属性设置)。

If you try to create new workbook, try like this; 如果您尝试创建新的工作簿,请尝试执行以下操作;

Workbook newWorkbook = this.Application.Workbooks.Add(missing);

I'm sorry I could not find the link earlier. 抱歉,我之前找不到该链接。 I used EPPlus supports. 我使用了EPPlus支持。 http://epplus.codeplex.com http://epplus.codeplex.com

Thanks for trying to help! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM