Click here to Skip to main content
16,016,425 members
Home / Discussions / C#
   

C#

 
GeneralRe: "New" Pattern Pin
Gerry Schmitz15-Sep-23 12:40
mveGerry Schmitz15-Sep-23 12:40 
QuestionHow to .sql scripts during application installation Pin
Member 1128773113-Sep-23 1:07
Member 1128773113-Sep-23 1:07 
AnswerRe: How to .sql scripts during application installation Pin
Richard Andrew x6413-Sep-23 3:32
professionalRichard Andrew x6413-Sep-23 3:32 
AnswerRe: How to .sql scripts during application installation Pin
RedDk13-Sep-23 8:56
RedDk13-Sep-23 8:56 
AnswerRe: How to .sql scripts during application installation Pin
Eddy Vluggen14-Sep-23 14:32
professionalEddy Vluggen14-Sep-23 14:32 
QuestionData Export to Excel using HSSFWorkbook Works Locally but Not on Test Pin
Fokwa Divine5-Sep-23 4:10
Fokwa Divine5-Sep-23 4:10 
AnswerRe: Data Export to Excel using HSSFWorkbook Works Locally but Not on Test Pin
Ron Nicholson5-Sep-23 4:43
professionalRon Nicholson5-Sep-23 4:43 
AnswerRe: Data Export to Excel using HSSFWorkbook Works Locally but Not on Test Pin
Richard Deeming5-Sep-23 4:48
mveRichard Deeming5-Sep-23 4:48 
Fokwa Divine wrote:
Also, we have checked the logs and there are no error messages to help point us to the root cause of the issue
Well, that's hardly surprising - your code swallows any exceptions that are thrown! D'Oh! | :doh:
Fokwa Divine wrote:
C#
catch (Exception ex)
{ 
    // Exception is swallowed; no details will be logged.
}

At a guess, the server has response buffering turned off for performance reasons. As a result, the Stream returned from Context.Response.Body is not "seekable", and the wb.Write call is failing for that reason.

Looking through the NPOI examples repo[^], you should write the workbook to a MemoryStream first, then copy that to the response.

Also, there's no need to use HttpContextAccessor within your view; you already have access to the current response via the Context.Response property.

I would strongly recommend generating the workbook in your controller, or preferable a service that you call from your controller, and sending it back to the client in a custom response type.

For example:
C#
public HSSFExcelResult : FileStreamResult
{
    private const ExcelMimeType = "application/vnd.ms-excel";
    
    private static Stream SaveWorkbook(HSSFWorkbook workbook)
    {
        ArgumentNullException.ThrowIfNull(workbook);
        
        MemoryStream ms = new();
        workbook.Write(ms);
        ms.Seek(0L, SeekOrigin.Begin);
        return ms;
    }

    public HSSFExcelResult(HSSFWorkbook workbook) : base(SaveWorkbook(workbook), ExcelMimeType)
    {
    }
}
C#
[HttpGet]    
public ActionResult ExportSummaryToExcel(string startDate, string endDate, string postalCode)
{
    HSSFWorkbook workbook = _summaryByAgeStagePostalCodeService.ExportSummaryToExcel(startDate, endDate, postalCode);
    return new HSSFExcelResult(workbook) { FileDownloadName = "Summary.xls" };
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

AnswerRe: Data Export to Excel using HSSFWorkbook Works Locally but Not on Test Pin
Fokwa Divine5-Sep-23 7:12
Fokwa Divine5-Sep-23 7:12 
GeneralRe: Data Export to Excel using HSSFWorkbook Works Locally but Not on Test Pin
jschell6-Sep-23 6:28
jschell6-Sep-23 6:28 
QuestionBluetooth set as com port in c# Pin
Jan 19472-Sep-23 13:54
Jan 19472-Sep-23 13:54 
AnswerRe: Bluetooth set as com port in c# Pin
Richard MacCutchan2-Sep-23 21:04
mveRichard MacCutchan2-Sep-23 21:04 
GeneralRe: Bluetooth set as com port in c# Pin
Jan 19472-Sep-23 21:47
Jan 19472-Sep-23 21:47 
GeneralRe: Bluetooth set as com port in c# Pin
Richard MacCutchan2-Sep-23 22:13
mveRichard MacCutchan2-Sep-23 22:13 
GeneralRe: Bluetooth set as com port in c# Pin
Jan 19472-Sep-23 23:42
Jan 19472-Sep-23 23:42 
GeneralRe: Bluetooth set as com port in c# Pin
Richard MacCutchan3-Sep-23 2:23
mveRichard MacCutchan3-Sep-23 2:23 
GeneralRe: Bluetooth set as com port in c# Pin
Jan 19473-Sep-23 2:35
Jan 19473-Sep-23 2:35 
GeneralRe: Bluetooth set as com port in c# Pin
Richard MacCutchan3-Sep-23 2:43
mveRichard MacCutchan3-Sep-23 2:43 
GeneralRe: Bluetooth set as com port in c# Pin
Jan 19473-Sep-23 11:44
Jan 19473-Sep-23 11:44 
GeneralRe: Bluetooth set as com port in c# Pin
Richard MacCutchan3-Sep-23 2:54
mveRichard MacCutchan3-Sep-23 2:54 
GeneralRe: Bluetooth set as com port in c# Pin
Jan 19473-Sep-23 11:38
Jan 19473-Sep-23 11:38 
QuestionNullable foreign key causes runtime error in OData service Pin
Alex Wright 202229-Aug-23 5:42
Alex Wright 202229-Aug-23 5:42 
QuestionRe: Nullable foreign key causes runtime error in OData service Pin
Richard MacCutchan29-Aug-23 6:21
mveRichard MacCutchan29-Aug-23 6:21 
AnswerRe: Nullable foreign key causes runtime error in OData service Pin
Alex Wright 202229-Aug-23 6:47
Alex Wright 202229-Aug-23 6:47 
GeneralRe: Nullable foreign key causes runtime error in OData service Pin
Dave Kreskowiak29-Aug-23 11:52
mveDave Kreskowiak29-Aug-23 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.