I'm using Server.Transfer and in the 2nd place I have a number of labels that are updated with a Request.Form["textbox_text"];
This all works really well, but the problem is I also want to write the content in that textbox to file
like a word document using this method
Response.Clear();
Response.AddHeader("Content-disposition", "attachment; filename=Word.doc");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application / vnd.ms -word";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
htw.Write("<table><h2><b>[ Text ]</b></h2><br>" + TextBox_name.Text + "</table>");
Response.Write(sw.ToString());
Response.End();
But whenever I check the file it will not have anything written on it. I've even tried to save the value of a Request.Form to a static variable and then write that variable but without any success.
How are you using Server.Transfer()? Post that code, make sure you are using the overload that preserves the form values:
Server.Transfer("page.aspx", true);
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();
I have a working Excel Report code. Still, when I click the button the file goes directly to my download folder, without giving me the option to change its name or selecting where I want to save it.
Code is the following:
public void GetExcel()
{
var list = (IList<LeaseViewModel>)Session["currentList"];
var grd = new GridView { DataSource = list, AutoGenerateColumns = true };
grd.DataBind();
Response.ClearContent();
Response.AddHeader("Content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Response.AddHeader("content-disposition", "attachment;filename=Leases.xls");
Response.ContentType = "application/excel";
var swr = new StringWriter();
var tw = new HtmlTextWriter(swr);
grd.RenderControl(tw);
Response.Write(swr.ToString());
Response.Flush();
Response.End();
tw.Close();
swr.Close();
}
Can somebody please indicate what should I change in order to have the window popping?
Thanks a lot.
Thanks for everybody highlighting this is a browser-configuration issue.
I thought that once I have the definition of the default name in the code I could also define it to always ask what is the name I want. I tested in different browsers though, and fact is you were right, thanks for identifying it and noticing me.
Regarding the default name issue, here is how I solve it:
Before:
Response.AddHeader("content-disposition", "attachment;filename=Leases.xls");
After:
Response.AddHeader("content-disposition", "attachment;filename=" + Session["listName"] + "_Report.xls");
Now regardless the report I export, the name is always customized.
Hope it helps other newbies like me (:
I am currently formatting a Date for a specific Excel file Export from a DataSet/DataGrid.
The Date is formatted like so:
DateTime date = Convert.ToDateTime(entry.Date);
string formatdate = String.Format("{0:yyyy/MM/dd}", date);
Once creating the DataSet is said and done, I use the following code to Export the DataSet to an Excel file:
public static void ExportDStoExcel(DataSet ds, string filename)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
My only problem is once I export this to Excel, Excel Auto-Formats the Dates like this: MM/DD/YYYY instead of YYYY/MM/DD.
I understand this could be achieved manually by opening in Excel, but the Export is being built into an Automated System and needs to be hard coded.
Is there any way of bypassing Excel's DateTime Auto-Formatting?
I had the same issue and solved it by adding a non breaking space ( ) in front of the text. Stopped Excel from auto-formatting. Not the cleanest solution but did the trick for me...
Right now you are just outputting HTML table, that Excel interprets how it likes. You'd have bring yourself down to Excel's level to be able to specify column's properties (set type to Text instead of General).
This means that you need to generate actual xls file (there are various libraries out there for that). Or (if restriction to Office 2010 is acceptable) got with Open XML format which you can write with regular .NET API.
You can style excel cells with mso-number-format
mso-number-format:"\\#"
\# will tell excel to treat all data in text format only. So auto format won't happen.
Please update your code like this:
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
response.Write("<head><style> td {mso-number-format:\\#;} </style></head><body>");
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.Write("</body></html>");
response.End();
}
}
OR
you can try with specific date format also.
Refer: http://cosicimiento.blogspot.in/2008/11/styling-excel-cells-with-mso-number.html
mso-number-format:"yyyy\/mm\/dd"
i m trying to generate an excel file from my web page but i have some issues with Turkish chars such as "İ" "ğ" when i open the file some chars are incorrect (İ seems Ä°) here is my code
gvExpRequests.DataSource = dsExpRequests;
gvExpRequests.DataBind();
gvExpRequests.GridLines = GridLines.Both;
Page.EnableViewState = false;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=export.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = Encoding.UTF8;
Response.BinaryWrite(Encoding.UTF8.GetPreamble());
StringWriter yaz = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(yaz);
gvExpRequests.RenderControl(htw);
i don't know what's wrong with here i tried a lot of encoding but i got same results every time i want to try something different to do this are there any another way to export a excel file from a gridview