0

嗨,我使用 Microsoft.Office.Interop.Excel dll 编写了代码,但是当我发布我的网站时它不支持在线。然后我发现在线支持 EPPlus dll 女巫,但我在转换代码以使用这个新 dll 时遇到了麻烦。

使用 Interop.Excel 的旧代码:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[8];

public void ToSpreadSheet()
{ 
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(tempFolderPathAlt + "dvforms\\InvestecTemplate.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

xlWorkSheet[0] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet[1] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); 
xlWorkSheet[2] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3); 
xlWorkSheet[3] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4); 
xlWorkSheet[4] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(5); 
xlWorkSheet[5] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(6); 
xlWorkSheet[6] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(7); 
xlWorkSheet[7] = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(8); 
}

这很好用,但现在我需要使用 EPPlus dll,但我遇到了问题。

 using Excel = OfficeOpenXml;

    Excel.ExcelPackage xlApp;
    Excel.ExcelWorkbook xlWorkBook;
    Excel.ExcelWorksheet[] xlWorkSeet = new Excel.ExcelWorksheet[8];

    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(tempFolderPathAlt + "dvforms\\InvestecTemplate.xlsx");
    Excel.ExcelPackage xlApp = new Excel.ExcelPackage(stream);

//This is where the problems begin, xlWorkBook.Worksheets.Add(1) is all underlined in red.
    xlWorkSeet[0] = (Excel.ExcelWorksheet)xlWorkBook.Worksheets.Add(1); 

如何从我的 Excel 模板加载工作表?我不确定我是否正确地完成了上述操作,请帮助我非常需要它。

提前致谢。

4

2 回答 2

0
FileInfo existingFile = new FileInfo(filePath);

            using (var package = new ExcelPackage(existingFile))
            {
                ExcelWorkbook workBook = package.Workbook;

                if (workBook != null)
                {
                    if (workBook.Worksheets.Count > 0)
                    {
                        int i = 0;
                        foreach(ExcelWorksheet worksheet in workBook.Worksheets)
                        {
                            xlWorkSeet1[i] = worksheet;
                            i = i + 1;
                        }

                    }
                }
于 2012-10-12T11:18:53.157 回答
-1

Add() 函数接受一个字符串参数,该参数是工作表的名称。尝试:

xlWorkSeet[0] = (Excel.ExcelWorksheet)xlWorkBook.Worksheets.Add("Sheet1");
于 2012-10-11T11:27:21.047 回答