Facing issue with exporting CSV using filehelper.dll - asp.net

I am exporting CSV from my asp.net web application using filehelper dll.
My code is as follows
this.Response.Clear();
this.Response.ClearContent();
this.Response.ClearHeaders();
Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "attachment;
filename=" + "Unicode_CSV_Demo.csv");
DemoCSVcls DemoCSVcls = new DemoCSVcls();
List<DemoCSVcls> CSVTextList = new List<DemoCSVcls>();
DemoCSVcls.Text1 = "‘tipping off’";
DemoCSVcls.Text2 = "ʼ";
CSVTextList.Add(DemoCSVcls);
var engine = new FileHelperEngine<DemoCSVcls>(Encoding.UTF8);
string finalString = engine.WriteString(CSVTextList);
Response.BufferOutput = true;
Response.Write(finalString);
Response.Flush();
Response.End();
In above code I am passing text "‘tipping off’" but when my csv opens in default csv viewer Excel2016 then it appear like follow ‘tipping off’
but same file I am opening in notepad++ or in notepad then it appears as it is like "‘tipping off’". even its encoding is showing in Notepad++ is "UTF-8"
’ these text render for some specific characters only in excel.
How can I stop rendering ’ this chracters in excel? , is there any solution to avoid this?

Related

Getting jibberish when attempting to open DOCX in a browser

Using a memory stream & the correct MIME type with Response.ContentType. Special characters are displayed instead of prompting to open a Word document. I'm using:
using (MemoryStream ms = new MemoryStream())
{
doc.SaveAs(ms);
Response.Clear();
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
ms.WriteTo(Response.OutputStream);
Response.End();
}
Here's an example of the output I get now rather than a prompt to open a Word document:
Turns out it was not a MIME or Response.ContentType issue at all. Being an MVC application, I was attempting to open the FileResult in a modal. Copy/Paste fail, as other buttons worked this way to display content. Not a Word document!

Getting a corrupted XLSX file when writing it to Response.OutputStream

In ASP.Net, I'm using NPOI to write save to an Excel doc and I've just moved to version 2+. It worked fine writing to xls but switching to xlsx is a little more challenging. My new and improved code is adding lots of NUL characters to the output file.
The result is that Excel complains that there is "a problem with some content" and do I want them to try to recover?
Here is a pic of the xlsx file that was created from my Hex editor:
BadXlsxHexImage Those 00s go on for several pages. I literally deleted those in the editor until the file opened without an error.
Why does this code add so many NULs to this file??
using (var exportData = new MemoryStream())
{
workbook.Write(exportData);
byte[] buf = exportData.GetBuffer();
string saveAsFileName = sFileName + ".xlsx";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}; size={1}", saveAsFileName, buf.Length.ToString()));
Response.Clear();
Response.OutputStream.Write(buf, 0, buf.Length);
exportData.Close();
Response.BufferOutput = true;
Response.Flush();
Response.Close();
}
(I've already tried BinaryWrite in place of OutputStream.Write, Response.End in place of Response.Close, and setting Content-Length with the length of the buffer. Also, none of this was an issue writing to xls.)
The reason you are getting a bunch of null bytes is because you are using GetBuffer on the MemoryStream. This will return the entire allocated internal buffer array, which will include unused bytes that are beyond the end of the data if the buffer is not completely full. If you want to get just the data in the buffer (which you definitely do), then you should use ToArray instead.
That being said, why are you writing to a MemoryStream at all? You already have a stream to write to: the OutputStream. Just write the workbook directly to that.
Try it like this:
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", saveAsFileName));
workbook.Write(Response.OutputStream);
Response.Flush();
Response.Close();

Download xml with its xsl file

I want to download xml file along with xsl(stylesheet).
My code for downloading xml file is as below:
XPathDocument myXPathDoc = new XPathDocument("myxml.xml");
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load("myxsl.xsl");
XmlTextWriter myWriter = new XmlTextWriter("Result.html", null);
myXslTrans.Transform(myXPathDoc, null, myWriter);
string strFullPath = Server.MapPath("Result.html");
string strContents = null;
System.IO.StreamReader objReader = default(System.IO.StreamReader);
objReader = new System.IO.StreamReader(strFullPath);
strContents = objReader.ReadToEnd();
objReader.Close();
//attach that XML file and download on local machine
string attachment = "attachment; filename=" + myWriter;
Response.ClearContent();
Response.ContentType = "text/html";
Response.AddHeader("content-disposition", attachment);
Response.Write(strContents);
I have searched in google, but not able to find a solution. Give some idea regarding this
But its giving exception like
The process cannot access the file '~mypath\Result.html' because it is being used by another process.
Do you want to write the xml to the response, or the xml with the xsl applied to it? If it is the latter, check this link:
Applying XSLT to XML in C#
If you want to just return the raw XML, then your code seems to be doing that already. However, the title of your question is a little misleading because you indicate you want to download 2 files with 2 request, which may be done with MIME but I fail to see the use of this. If a client is requesting an XML and XSL file, why not just apply them together?
I have added one html file and done like below
string strFullPathXml = Server.MapPath("myxml.xml");
string strFullPathXsl = Server.MapPath("myxsl.xsl");
string strFullPathHtml = Server.MapPath("Result.html");
XPathDocument xPathDoc = new XPathDocument(strFullPathXml);
XslCompiledTransform xslTrans = new XslCompiledTransform();
xslTrans.Load(strFullPathXsl);
XmlTextWriter xWriter = new XmlTextWriter(strFullPathHtml, null);
xslTrans.Transform(xPathDoc, null, xWriter);
xWriter.Close();
Response.ContentType = "text/html";
Response.AppendHeader("Content-Disposition", "attachment; filename=Result.html");
Response.ClearContent();
Response.WriteFile(strFullPathHtml);
Response.Flush();
Response.End();
After that its downloading html file which is in human readable format.

Opening a word file after downloading from code behind(asp.net)

I have written code to open a Word document after downloading it in code behind. The document is opening fine, but it is not saving in Word format. When I am going to open it, it is asking for selecting the format to open the file.
The code is below:
string FullFilePath = "D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition", "inline; filename=\"" + txtDate.Text + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}
Set your content type to application/msword.
Refer: Microsoft Office MIME Types
You are not specifying an extension when sending the file name.
If your file saves without an extension, you will get the prompt asking for the application to use to open it.
Also, use the "Content-Disposition", "Attachment" if you want to tell the browser to save the file. inline will make the browser attempt to open the file in Word directly.
string FullFilePath =//path of file //"D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "Attachment; filename=\"" + txtDate.Text + ".doc\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}

Export to Excel file as .zip to reduce file size. How to compress xmldata before sending it to the client?

I like to compress the xmldata before the data is provided to the client as an excel file. I am trying to compress and deliver it as a .zip file. its not working Here's my code below. Please help
I tried compressing it, converting it to bytes etc etc. The issue with below code is, the XSL transformation is not happening properly and the output excel file is raw xml with some .net exception at the end. (that's what I see on the .xls file that's downloaded at the end)
Before I started working on compression my below code was working fine that gives properly formatted excel file from the xml input. the excel file is so nice you can't even tell it was from XML.
pLEASE HELP with compression
Dim attachment As String = "attachment; filename=DataDownload.xls"
Response.ClearContent()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/vnd.ms-excel"
'Response.ContentType = "text/csv"
Response.Charset = ""
Dim ds As New DataSet()
Dim objXMLReader As XmlReader
objXMLReader = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteXmlReader(ConnectionString, CommandType.StoredProcedure, "SPName")
ds.ReadXml(objXMLReader)
Dim xdd As New XmlDataDocument(ds)
Dim xt As New XslCompiledTransform()
'xt.Transform(xdd, Nothing, Response.OutputStream)
Dim bytearr() As Byte
bytearr = System.Text.Encoding.Default.GetBytes(xdd.OuterXml)
Dim objMemStream As New MemoryStream()
objMemStream.Write(bytearr, 0, bytearr.Length)
objMemStream.WriteTo(Response.OutputStream)
xt.Transform(xdd, Nothing, Response.OutputStream)
Response.End()
Why you don't use XLSX-Format instead of XLS? It is already XML-Data compressed in a ZIP file. You can rename a XLSX file to ZIP to verify this.
Updated: By the way, ContentType in the case should be "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" instead of "application/vnd.ms-excel".

Resources