|
I'm doing something similar with product called Gembox. In a nutshell, I create the excel file and save it to a path under the website. In my case it is normally a folder called ExcelTemp. I save the file to that folder then use the following code to save it on the enduser's pc. This code will prompt them to save it unless the browser is set to automatically put it somewhere.
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(fileName));
Response.Charset = "";
Response.WriteFile(fileName);
Response.End();
The code need the fileName as a string.
HTH
Jack of all trades, master of none, though often times better than master of one.
|
|
|
|
|
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!
Fokwa Divine wrote:
catch (Exception ex)
{
}
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:
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)
{
}
}
[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
|
|
|
|
|
We finally found the issue. This template file
SummaryByAgeStagePostalBlank.xls was not getting copied along when we publish the project to our Test environment. We set the property for the file on Visual Studio to always copy if newer. The initial setting was "Do not copy".
|
|
|
|
|
Your code needs better error detection.
As already noted do not eat exceptions.
Also do checks to make sure you actually did something. For example, did you read the file but it was empty?
|
|
|
|
|
Hi Guys
me again, old man again .. I have used Scanner set as com port and I like to change it to use Bluetooth, used as com port.
I have register device and change it to use it as com port in Windows 11. I am using SerialPort and add
SerialErrorReceivedEventHandler . This event never happens. I cannot find any suggestion. Can you help me please ?
Regards John
|
|
|
|
|
Jan 1947 wrote: This event never happens. Perhaps there are no errors.
|
|
|
|
|
Hi Richard.
no errors, The Receive Data Event not working. the com port is in /foreach(string port in ports) / as well it shows as com port in Device Manager as 'Standard Serial over Bluetooth link (COM4)'. I use standard Serial port structure <serialport(port)>. It works as HID, when I scan barcode while in Note Pad ?!!?? In device manager I have its HID disabled. When I change the driver to do as com port, my software works. I have an issue with the com port driver supplied with the scanner, and this is the reason I like to change it.
Regards John
|
|
|
|
|
If it is a problem with the device you are connecting to then there is very little anyone here can do to help.
|
|
|
|
|
Hi Richard
maybe I have not been clear. I do not want to use the faulty driver as it has problems. This is why I like to use Scanner via Bluetooth and redirected to operate as com port. The Bluetooth driver is Microsoft and redirection is done via Windows 11, so no faulty driver to be used.
regards John
|
|
|
|
|
Sorry, maybe I am being dense, but I still do not understand what you are asking for.
|
|
|
|
|
Hi Richard
I want to use Bluetooth connection and use it to communicate as serial port with <serialdatareceivedeventhandler> to operate. Maybe I am asking for something which cannot be done
appreciate you help
regards John
|
|
|
|
|
|
Hi Richard
Yes this is what I am using, but the event is never triggered. Serial port exists.
thanks again
regards John
|
|
|
|
|
|
Hi Richard
great, yes this is how I use it, only I would like to use DataReceiveEvent. I will try to do it using the loop, may be this is the solution.
Thanks you very much
Regards John
|
|
|
|
|
I have used OData in my asp.net core project. I have problem with nullable foreign keys. When I want to use expand in url query, there is an exception that says:
System.InvalidOperationException: Nullable object must have a value.
I have provided a simple reproducible project in GitHub GitHub - codeblock88/temp: This repository is used for debugging.[^]repository.
Please help me to solve this problem.
|
|
|
|
|
If a key is nullable then how can it be used to maintain referential integrity?
|
|
|
|
|
I want to expand it for records are not null.
|
|
|
|
|
It's not "records" that can be null, but individual fields in the record.
You're not making much sense, so you're going to have to do a much better job of explaining what you're trying to do.
As for the error you're getting, show the code that's throwing it.
|
|
|
|
|
I have no idea what you mean by that, or what it has to do with your original question.
|
|
|
|
|
There's nothing in that project that would actually call your API and produce that error. Where's the actual reproduction of the error?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
"Nullable foreign keys". Sounds like the definition of an orphan.
(You need a "null' parent in those cases; if you want to stay sane)
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Replace the null with a zero and filter them out in your query.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Hi Guys,
I am not expert and will be glad to get some help. I wrote software, where I am using Barcode reader, com port, in number of Windows forms. I am switching receive data on Form Activation and disabling it on Form deactivated. it has worked well for number of months, but now I am occasionally get error, Com port is not accessible. Received I use Invoke.
<pre> private void CheckForUsbDevice()
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
if ((port == "COM3") || (port == "COM4"))
{
if (AcdStart.mySerialPort == null)
{
AcdStart.mySerialPort = new SerialPort(port)
{
BaudRate = 9600,
Parity = Parity.None,
StopBits = StopBits.One,
DataBits = 8,
Handshake = Handshake.None,
RtsEnable = true
};
}
AcdStart.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
if (!AcdStart.mySerialPort.IsOpen)
{
AcdStart.mySerialPort.Close();
AcdStart.mySerialPort.Open();
}
PortTextBox.Text = (string.Format("Port {0}", port));
break;
}
}
}
private void RxPurchaseOrder_Deactivate(object sender, System.EventArgs e)
{
if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
AcdStart.mySerialPort.DiscardInBuffer();
if (AcdStart.mySerialPort != null)
{
AcdStart.mySerialPort.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedHandler);
AcdStart.mySerialPort.Close();
}
AcdStart.mySerialPort = null;
PauseNow = true;
this.BackColor = Color.LightSalmon;
}
private void RxPurchaseOrder_Activated(object sender, EventArgs e)
{
if ((AcdStart.mySerialPort != null) && (AcdStart.mySerialPort.IsOpen))
{
AcdStart.mySerialPort.DiscardInBuffer();
if (AcdStart.mySerialPort.IsOpen)
AcdStart.mySerialPort.Close();
AcdStart.mySerialPort = null;
}
PortTextBox.Text = "";
CheckForUsbDevice();
PauseNow = false;
this.BackColor = Color.LightSteelBlue;
}
I hope I have indicated flow of the basics. I am self learning pensioner ... apologize in advance and thanks for any help and suggestions.
regards John
|
|
|
|
|
What is the exact error message, and where is it generated?
|
|
|
|