工作表位置超出范围.连接已关闭.使用EPPLUS时 [英] Worksheet position out of range. Connection Closed. When using EPPLUS
问题描述
我正在尝试使用EPPLUS 4.0.3.将XLSX文件作为模板打开(我什至使用了空白的XLSX文件).
I am trying to open an XLSX file as a template (I have even used a blank XLSX file), using EPPLUS 4.0.3.
如果我不打开模板文件(空白文件或真实文件),而只是创建一个新工作簿并创建一个工作表,则它可以正常工作.或者,如果我打开模板文件并创建一个新的工作表,则可以正常工作.只有当我尝试访问模板中的 FIRST 工作表时,才会出现错误: 工作表位置超出范围.
If I do not open the template file (blank or the real one) and just create a new workbook and create a sheet, it works fine. OR if I open the template file, and create a NEW worksheet, then it works fine. It is only when I try to access the FIRST sheet in the template that I get the error: Worksheet position out of range.
像这样访问第一个工作表:workBook.Worksheets.First()
不起作用.
Accessing the first worksheet like this: workBook.Worksheets.First()
DOES NOT WORK.
首先不再是定义.
First is no longer a definition.
因此,我尝试通过名称和此方法workBook.Worksheets[1]
使用0和1来访问第一个工作表,以尝试获取第一个工作表.
So I tried accessing the first worksheet by name and by this method workBook.Worksheets[1]
using both 0 and 1 to try to get the first sheet.
我的代码:
var existingTemplate = new FileInfo(_ExcelTemplateFilePath);
using (ExcelPackage p = new ExcelPackage(existingTemplate)) {
// Get the work book in the file
ExcelWorkbook workBook = p.Workbook;
ExcelWorksheet ws = workBook.Worksheets[1];
// MY OTHER EXCEL CELL CODE HERE
}}
有人知道如何访问Excel和Excel文件的第一张纸吗?
Does anyone know how to access the first sheet of and Excel file?
推荐答案
我能够通过使用名称而不是索引来引用工作表来解决此问题.
I was able to get around this issue by referring to the worksheet by name, rather than index.
var oSheet = package.Workbook.Worksheets["My Worksheet Name"];
这篇关于工作表位置超出范围.连接已关闭.使用EPPLUS时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!