将复制模板复制到新工作表

时间:2016-01-15 15:06:39

标签: c# epplus

我已经搜索过,但似乎无法找到适合我情况的答案。我正在创建一个工作簿,需要为主查询的每个结果添加一个新选项卡,我希望每个选项卡都使用该模板。模板工作正常,但我无法弄清楚如何使用它来创建新的工作表。

        ExcelWorksheet worksheet = packageInUse.Workbook.Worksheets[1];
        worksheet.Name = "Template";
        var templateSheet = packageInUse.Workbook.Worksheets["Template"];
        int rowNbr = 1;


        using (OleDbConnection reportConn = new OleDbConnection(IniReadValue("Main", "reportDbConn")))
        {
            try
            {
                //first select divsisions based on NetPercent DESC to get them in the right order PER PAY PERIOD
                reportConn.Open();
                OleDbCommand divsionQry = new OleDbCommand( 
                    "SELECT " +
                    "DivSumClient.Division, DivSumClient.Client, DivSumClient.NetPercent, DivSumClient.PeriodEndDate " +
                    "FROM DivSumClient " +
                    "WHERE DivSumClient.Client = 'TOTAL' And DivSumClient.PeriodEndDate = ?  " +
                    "GROUP BY " +
                    "DivSumClient.PeriodEndDate, DivSumClient.Division, DivSumClient.Client, DivSumClient.NetPercent " +
                    "ORDER BY " +
                    "DivSumClient.NetPercent DESC "
                     , reportConn);

                divsionQry.Parameters.AddWithValue("?", endDate);

                OleDbDataReader reader = divsionQry.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                     //copy template and start new tab for each division
                     packageInUse.Workbook.Worksheets.Add(reader["Division"].ToString(), templateSheet);
                     worksheet = packageInUse.Workbook.Worksheets[reader["Division"].ToString()];    

我收到错误,该对象引用未设置为此行上的对象实例:packageInUse.Workbook.Worksheets.Add(reader [“Division”]。ToString(),templateSheet);

这是我用来加载模板的代码。它非常简单,工作正常,但使用它在工作表中创建新选项卡是行不通的。

private void runDivSum()
        {
            System.IO.MemoryStream outputFileStream = new MemoryStream();
            System.IO.MemoryStream templateFilestream = new MemoryStream(System.IO.File.ReadAllBytes(appPath + @"\DivisionSummaryTemplate.xlsx"));
            using (ExcelPackage package = new ExcelPackage(outputFileStream, templateFilestream))
            {
                fillDivisionSummary(startDt, endDt);
                writeDivSummary(package, startDt, endDt);
                package.Save();
            }

            // Write content of memory stream into file stream
            string fileName = @"c:\temp\DivisionSummary.xlsx";

            using (var fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                outputFileStream.WriteTo(fs);
            }

            Process excel = new Process();
            excel.StartInfo.FileName = "excel.exe";
            excel.StartInfo.Arguments = @"c:\temp\DivisionSummary.xlsx";
            excel.Start();

            SaveFileToSharepoint(fileName);
        }

我明白了。这是神奇的组合。我正在加载模板作为第一个工作表,我停止尝试命名该工作表并直接引用它。在程序结束时,我只是从工作簿中删除第一张表。

ExcelWorksheet worksheet = packageInUse.Workbook.Worksheets[1];
        int rowNbr = 1;
        worksheet.PrinterSettings.RepeatRows = new ExcelAddress("1:1");


                OleDbDataReader reader = divsionQry.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                     //copy template and start new tab for each division
                     rowNbr = 1;
                     packageInUse.Workbook.Worksheets.Add(reader["Division"].ToString(), packageInUse.Workbook.Worksheets[1]);
                     worksheet = packageInUse.Workbook.Worksheets[reader["Division"].ToString

0 个答案:

没有答案