How to create a Excel file using OfficeOpenXML in ASP.NET Core 7 Web API and open that file in Excel 2016

148 views Asked by At

I have created an Excel file using OfficeOpenXML, the file was successfully created, but when I try to open it in Excel 2016, some of the data shows as a black screen, but it is opening properly in WPS Office. I am creating this file in an ASP.NET Core 7.0 Web API using OfficeOpenXml and C#.

public static ExcelWorksheet GetExcel(BillingExport list,
    BillingReportPersonalDetails persdetail,
    List<BillingReportCPTcodesDetails> data,
    List<BillingReportCPTcodesDetails> Summarydata, ExcelPackage
    package, string excelname)   
{
          ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(excelname);
          int counter = 1;
    
          worksheet.Cells[counter, 1].Value = "Reimbursement Report RPM (Summary)";
          worksheet.Cells[counter, 1].Style.Font.Bold = true;
          worksheet.Cells[counter, 1].Style.Font.Size = 12;
          ++counter;
    
          worksheet.Cells[counter, 1].Value = "Client Name:";
          worksheet.Cells[counter, 1].Style.Font.Bold = true;
          worksheet.Cells[counter, 2].Value = persdetail.ClientName;
          ++counter;
    
          worksheet.Cells[counter, 1].Value = "Address:";
          worksheet.Cells[counter, 1].Style.Font.Bold = true;
          worksheet.Cells[counter, 2].Value = persdetail.Address;
          ++counter;
    
          worksheet.Cells[counter, 1].Value = "Phone No:";
          worksheet.Cells[counter, 1].Style.Font.Bold = true;
          worksheet.Cells[counter, 2].Value = persdetail.Phone;
          ++counter;
    
          worksheet.Cells[counter, 1].Value = "Activity Dates:";
          worksheet.Cells[counter, 1].Style.Font.Bold = true;
          worksheet.Cells[counter, 2].Value = $"{list.FromDate} {list.ToDate}";
          worksheet.Cells[counter, 2].Style.Font.Bold = true;
    
          worksheet.Cells[counter, 3].Value = "Use this range as Customer Ref in Sales Order";
          worksheet.Cells[counter, 3].Style.Font.Bold = true;
          ++counter;
    
          counter++;
    
    
          worksheet.Cells[counter, 8].Value = "Use this information to create the sales order";
          worksheet.Cells[counter, 8].Style.Font.Bold = true;
          ++counter;
    
          int colHeaderCounter = 1;
    
          worksheet.Cells[counter, colHeaderCounter].Value = "Patient";
          colHeaderCounter++;
    
          worksheet.Cells[counter, colHeaderCounter].Value = "DOB";
          colHeaderCounter++;
    
          worksheet.Cells[counter, colHeaderCounter].Value = "MNR";
          colHeaderCounter++;
    
          worksheet.Cells[counter, colHeaderCounter].Value = "Applicable CPT Codes";
          colHeaderCounter++;
    
          worksheet.Cells[counter, colHeaderCounter].Value = "Collectible fee";
          colHeaderCounter++;
    
          worksheet.Cells[counter, colHeaderCounter].Value = "Medicare Notes";
          colHeaderCounter++;
    
          var firstGroupRange = worksheet.Cells[counter, 1, counter, colHeaderCounter];
          SetCellBorders(firstGroupRange);
    
          worksheet.Cells[counter, colHeaderCounter + 1].Value = string.Empty;
          worksheet.Cells[counter, colHeaderCounter + 2].Value = string.Empty;
    
          int j = counter;
    
          worksheet.Cells[j, colHeaderCounter + 1].Value = "Applicable CPT Codes";
          worksheet.Cells[j, colHeaderCounter + 1].Style.Font.Bold = true;
    
          worksheet.Cells[j, colHeaderCounter + 2].Value = "Collectible fee";
          worksheet.Cells[j, colHeaderCounter + 2].Style.Font.Bold = true;
    
          worksheet.Cells[j, colHeaderCounter + 3].Value = "Count";
          worksheet.Cells[j, colHeaderCounter + 3].Style.Font.Bold = true;
    
          worksheet.Cells[j, colHeaderCounter + 4].Value = "Medicare Notes";
          worksheet.Cells[j, colHeaderCounter + 4].Style.Font.Bold = true;
    
          int secondGroupStartColumn = colHeaderCounter + 1;
          if (data.Count > 0)
          {
              var secondGroupRange = worksheet.Cells[j, secondGroupStartColumn, j, colHeaderCounter + 4];
              SetCellBorders(secondGroupRange);
          }
    
          int i = counter + 1;
    
          foreach (var row in data)
          {
              int colDataCounters = 1;
              worksheet.Cells[i, colDataCounters++].Value = row.PatientName;
              worksheet.Cells[i, colDataCounters++].Value = row.DOB;
              worksheet.Cells[i, colDataCounters++].Value = row.MNRNumber;
              worksheet.Cells[i, colDataCounters++].Value = row.ApplicableCPTCodes;
              worksheet.Cells[i, colDataCounters++].Value = row.Collectiblefee;
              worksheet.Cells[i, colDataCounters++].Value = row.MedicareNotes;
    
              var dataRowRange = worksheet.Cells[i, 1, i, colHeaderCounter + 2];
              SetCellBorders(dataRowRange);
    
              i++;
          }
    
          j++;
    
          foreach (var row in Summarydata)
          {
              int colDataCounters = colHeaderCounter + 1;
              worksheet.Cells[j, colDataCounters++].Value = row.ApplicableCPTCodes;
              worksheet.Cells[j, colDataCounters++].Value = row.Collectiblefee;
              worksheet.Cells[j, colDataCounters++].Value = row.Count;
              worksheet.Cells[j, colDataCounters++].Value = row.MedicareNotes;
              var summaryDataRowRange = worksheet.Cells[j, colHeaderCounter + 1, j, colHeaderCounter + 4];
              SetCellBorders(summaryDataRowRange);
              j++;
          }
    
          using (var range = worksheet.Cells[j, 1, j, colHeaderCounter + 4])
          {
              SetCellBorders(range);
          }
          j++;
    
          for (int columnIndex = 1; columnIndex <= colHeaderCounter + 2; columnIndex++)
          {
              int maxColumnWidth = worksheet.Cells[1, columnIndex, i - 1, columnIndex].Max(cell => cell.Text.Length);
              worksheet.Column(columnIndex).Width = maxColumnWidth + 2; // Add some extra width for padding
          }
          //worksheet.Cells[1, 1, i - 1, colHeaderCounter + 2].AutoFitColumns(0);
    
          using (var colorrange = worksheet.Cells[counter, 1, i - 1, colHeaderCounter + 2])
          {
              colorrange.Style.Fill.PatternType = ExcelFillStyle.Solid;
          }
    
          using (var range = worksheet.Cells[counter, 1, counter, colHeaderCounter + 2])
          {
              range.Style.Font.Bold = true;
          }
    
          using (var range = worksheet.Cells[i, 1, i, colHeaderCounter + 2])
          {
              range.Style.Font.Bold = true;
          }
    
          worksheet.View.PageLayoutView = false;
          return worksheet;
}
0

There are 0 answers