C# fileName display with spaces trouble - c#

Suppose I have a report Name as :
string reportName = "Facebook Network Performance Summary Report (By Region, By Content)";
and I would like to display that file name as excel
the below is my code to export the file
Response.AddHeader("Content-Disposition", "attachment; filename=" reportName + ".xls");
Response.AddHeader("Content-Length", l.ToString());
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(f);
Unluckily, thewhen reading the first space of my reportName, it only shows Facebook and no else.
Is there any wexisting API for ASP.NET or other methods to handle the spaces of fileNAme ?

Try wrapping the filename in double quotes.
string filename = reportName + ".xls";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

You need to wrap the filename in quotes:
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\".xls");

How about using verbatim string for this
string reportName = #"Facebook Network Performance Summary Report (By Region, By Content)";

try adding the # and/or double-double quotes before the double quotes to make c# read literals
Problem with spaces in a filepath - commandline execution in C#

Try this...
var fileName = HttpUtility.UrlEncode(document.FileName, Encoding.UTF8);

Related

Save japanese characters in CSV file and zip and save it to specific location

I want to save data, which is Japanese characters, in csv file. Then i want to zip the file and save it to a particular location.
Following is the code I am trying but it does not save the Japanese characters.
Can any one please let me know the issue in the code. And please tell me how to zip it and save it to a location.
var csvData = new StringBuilder();
csvData.AppendFormat("{0},{1},{2},{3},{4},{5},{6}",
"申込区分", "オリジナルカード番号", "スペアカード番号", "セットNO", "送り状番号",
"入金", "カード発行", "キャンセル", "フォームID", "フォームID別のシーケンス番号", "ご希望のカード T-ポイント付きかどうか",
"本人確認書類の種類", "本人確認書類の番号",
"申込No", "申込日時", "エージェント名", "エージェントテーブルのID", "お名前(姓)", "お名前(名)", "ローマ字(姓)",
"ローマ字(名)", "電話番号", "メールアドレス", "CCC専用会員ナンバー", "提携店会員ナンバー1", "提携店会員ナンバー2",
"携帯番号", "生年月日", "職業", "性別", "母親の旧姓", "ご利用通貨", "初回チャージ額(ご入金予定額)", "カード種別コード",
"通貨", "郵便番号", "都道府県", "都道府県ローマ字", "市郡区", "市郡区ローマ字", "町名", "町名ローマ字", "番地", "番地ローマ字",
"建物名/階数/部屋番号など", "建物名/階数/部屋番号などローマ字", "ご出発予定日", "渡航国", "渡航(利用)目的",
"渡航(利用)目的商品代金商品名", "渡航(利用)目的商品代金原産国", "渡航(利用)目的商品代金船積地", "渡航(利用)目的その他テキスト",
"同意事項1", "同意事項2", "同意事項3", "同意事項4", "マイナンバー確認の有無", "マイナンバー確認日付", "マイナンバー不備",
"本人確認書類不備",Environment.NewLine);
var response = System.Web.HttpContext.Current.Response;
response.BufferOutput = true;
response.Clear();
response.ClearHeaders();
response.ContentEncoding = Encoding.UTF8;
response.Charset = Encoding.UTF8.WebName;
response.AddHeader("content-disposition", "attachment;filename=Employee.CSV ");
response.ContentType = "text/csv";
response.Write(csvData.ToString());
response.End();

How to avoid downloading files to the download folder and download to other specific folder only in c#

I am trying to convert the gridview to excel and downloading the excel sheet to my computer's specific folder in my c# application.
The problem is: The file is getting downloaded at both the places.The destination folder and also the download folder.
the code for this is:
private void ExportToExcel(GridView GrdView, string fname)
{
Response.Clear();
Response.AddHeader("content-disposition", "inline;filename=" + fname + ".xls");
Response.Charset = "";
Response.ContentType = "application/OCTET-STREAM";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
string renderedGridView = stringWrite.ToString();
File.WriteAllText(#"C:\\Users\abc\\Desktop\" + fname + ".xls", renderedGridView);
Response.Write(stringWrite.ToString());
Response.End();
}
How to avoid the files getting downloaded to the download folder?
Please help!
Thank you
The answer would be to pick one: either render the control to a string and output to Response OR WriteAllText. If you want to use the WriteAllText to save the file to a determined location, then perform the action and pop up a notification to the user that the file was saved.

Illegal characters in path c# error

I am getting the error
Illegal characters in path
for the code below . Please help me out.
response.Clear();
response.Charset = "";
Response.ContentEncoding = System.Text.Encoding.Default;
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
string sFileName = "Testeeeeee.xls";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + sFileName + "\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = DS.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
string sPath = #"E:\CR-12\Test.xls";
File.WriteAllText(sw.ToString(), sPath);// I get the error at this line
response.End();
The parameters are inverted. Do it as follows:
File.WriteAllText(sPath,sw.ToString());
You've got the parameters to File.WriteAllText mixed up. The first one is the path, the second is the contents. You need
File.WriteAllText(sPath, sw.ToString());
The call to
File.WriteAllText(sw.ToString(), sPath);
has the wrong parameter order. It should be
File.WriteAllText(sPath, sw.ToString());
For others receiving this error, make sure you are using backslashes correctly since they are also escape characters. A single backslash can result in this error. Use 2 backslashes to properly output a single escaped backslash.
Ex:
System.IO.File.WriteAllText("C:\\file.txt", output);
The issue is probably a security related one. Your webpage wouldn't be default have access to the E drive.

ASP.NET: Write a table (and checkboxes) to a word document

I am writing an ASP.NET Application in c# and I'm trying to write a table to a word document.
I use currently the following code to write to a word doc:
string strContent = "This content goes to the word document";
string attach = "attachment; filename=wordtest.doc";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/msword";
HttpContext.Current.Response.AddHeader("content-disposition", attach);
HttpContext.Current.Response.Write(strContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
Now, my question is if anyone knows how I can write a table to a word document?
And second question is if anyone knows how to write checkboxes to a word doc (like you copy it from a website and paste it in word)
Thanks in advance!
Killerwes
Word can recognise HTML. So you can simply write any HTML to MS WORD with Response.Write(); Method. Here is code sample.
string strBody = "<html>" +
"<body>" +
"<div>Your name is: <b>" + txtName.Text + "</b></div>" +
"<table width="100%" style="background-color:#cfcfcf;"><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" +
"Ms Word document generated successfully." +
"</body>" +
"</html>";
string fileName = "MsWordSample.doc";
// You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);
Response.Write(strBody);

Can't export just a part of my aspx page on C#

I am trying to export just a table that is contained on my page. I have some filters above it but when exporting, all the text and filters are copied into this file.
This is the code:
tableCreateOrders a = new tableCreateOrders(dtReportColumns, 2, 10);
divResult.Controls.Add(a.displayTable());
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=orders.xls");
Response.Write(a.displayTable());
a.displayTable() is the object containing the table, but the filters are defined on the .aspx so there is no way the command take it. Is there anything I am doing wrong? I'll really appreciate any suggestion.
This is what you need. Remember to ClearContent before and End after.
Response.ClearContent();
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=orders.xls");
Response.Write(a.DisplayTable());
Response.End();

Categories