Analytics

顯示具有 xls 標籤的文章。 顯示所有文章
顯示具有 xls 標籤的文章。 顯示所有文章

2013年8月2日 星期五

[ASP.NET]解決 Gridview匯出excel時內容變成亂碼 (Export Gridview resolve when garbled excel content)


問題
Gridview匯出excel時內容變成亂碼



解決方法
public void ExportExcelFromGridView(GridView gv)
{
    string filename = "Excel1.xls";      
    HttpContext.Current.Response.ClearContent();
    //fix 亂碼加入下行
    HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gd.RenderControl(htw);
    HttpContext.Current.Response.Write(sw.ToString());
    HttpContext.Current.Response.End();
}

2013年5月29日 星期三

[C#]使用 數字取得對應的EXCEL欄位 (Using digital acquires the corresponding EXCEL columns)


問題
使用 數字取得對應的EXCEL欄位



解決方法
private string GetExcelColumnName(int columnNumber)
{
    int dividend = columnNumber;
    string columnName = String.Empty;
    int modulo;

    while (dividend > 0)
    {
        modulo = (dividend - 1) % 26;
        columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
        dividend = (int)((dividend - modulo) / 26);
    } 
    return columnName;
}

2012年4月24日 星期二

[ASP.NET]解決 匯出檔案時,檔名為亂碼的問題(Solve the problem of garbled file name when you export a file)


問題
匯出檔案時,檔名為亂碼的問題



解決方法
string saveName = “myExcel.xls”;
var savePath = AppDomain.CurrentDomain.BaseDirectory + "ExportExcel\\";
Byte[] binaryData = File.ReadAllBytes(savePath + saveName);
Response.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Expires = 0;
Response.Buffer = true;
Response.HeaderEncoding = System.Text.Encoding.Default;Response.ContentType = "application/octet-stream";
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + saveName);
Response.AddHeader("Content-length", binaryData.Length.ToString());
Response.BinaryWrite(binaryData);
Response.End();

2011年3月25日 星期五

[ASP.NET]解決 GridView 匯出Excel 發生Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."


問題

GridView 匯出Excel 發生Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."



解決方法

語法如下:
protected void btnExportExcel_Click(object sender, EventArgs e)
{ 
 if (GridView1.Rows.Count > 0) 
 { 
  string attachment = string.Format("attachment; filename=allocationInvoice{0}.xls" , DateTime.Now.ToString(("yyyyMMddHHmmss"))); 
  Response.ClearContent(); 
  Response.AddHeader("content-disposition", attachment); 
  Response.ContentType = "application/ms-excel";
  StringWriter sw = new StringWriter(); 
  HtmlTextWriter htw = new HtmlTextWriter(sw);
  GridView1.RenderControl(htw); 
  Response.Write(sw.ToString()); 
  Response.End(); 
 } 
}
但是會發生錯誤下:
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
解決方式override 原有的function:
public override void VerifyRenderingInServerForm(Control control)
{
 
}

[ASP.NET]使用 GridView 匯出完整EXCEL 解決Gridview分頁顯示不完全(Use Export GridView complete EXCEL solve Gridview Pagination incomplete)


問題
GridView 匯出完整EXCEL 解決Gridview分頁顯示不完全
當GridView有設定分頁的時候,會發生多頁的時候,EXCEL卻只會出某一頁,如下:
string attachment  = string.Format("attachment; filename=allocationInvoice{0}.xls" , DateTime.Now.ToString(("yyyyMMddHHmmss")));
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();




解決方法
解決方式其實很簡單,在respose到client前,重新指定GridView的PageSize就可以了!如下:
ReportViewPool.allocationInvoiceDataTable tb = DataLoad();//自訂義tb GridView1.DataSource = tb;
GridView1.PageSize = tb.Rows.Count;//重新指定頁面筆數
GridView1.DataBind();//指定以後依定要bind一次
string attachment = string.Format("attachment; filename=allocationInvoice{0}.xls" , DateTime.Now.ToString(("yyyyMMddHHmmss")));
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

2010年10月14日 星期四

[C#]使用 OleDbDataAdapter從Excel讀取資料再匯出EXCEL檔案(Using OleDbDataAdapter to read data from Excel files and then export EXCEL)


問題
使用 OleDbDataAdapter從Excel讀取資料再匯出EXCEL檔案



解決方法
OleDbConnection con = new OleDbConnection(DBPath);
OleDbDataAdapter da = new OleDbDataAdapter("Select * From [" + rbUploadOption.SelectedItem.Value + "$] ", con);
DataSet ds = new DataSet();
da.Fill(ds, "TEST");

GridView gvExport = new GridView();
gvExport.DataSource = ds;
gvExport.DataBind();

string ExportFilename = DateTime.Now.ToString("yyyyMMddHHmmss");
Response.Clear();
Response.AddHeader("content-disposition","attachment;filename=" + ExportFilename);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
Response.Charset = "uft8";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvExport.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString().Replace("<div>", "").Replace("</div>", ""));
Response.End();

2010年7月7日 星期三

[ASP.NET]DataGrid裡有樣板時匯出Excel時寫法(DataGrid was written while exporting Excel template when)


問題

DataGrid裡有樣板時匯出Excel時寫法

背景:匯出EXCEL之前已經有寫過一篇
問題:當GridView裡加入樣板,而樣板裡加入一個TextBox控制項,發現在匯出Excel的時候,整個aspx都被寫入Excel,一整個傻眼,

解決方法

改寫如下
Dim file_name As String = DateTime.Now.ToString("yyyyMMddHHmmss") & ".xls"
Dim myTextWriter As New System.IO.StringWriter

'寫入標題    
myTextWriter.WriteLine("姓名" & vbTab &"電話"&"vbTab&"備註") 

'以迴圈方式讀取DataGrid的值    
For i As Integer = 0 To dgData.Items.Count – 1
myTextWriter.WriteLine(dgData.Items(i).Cells(2).Text & vbTab _    
& dgData.Items(i).Cells(2).Text.Replace("&nbsp;", "") & vbTab _    
& CType(dgData.Items(i).FindControl("txtContract_Content"), TextBox).Text)

Next    

myTextWriter.Close()    
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls")    
Response.ContentType = "application/ms-excel"    
Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5")    
Response.Write(myTextWriter)    
Response.End()

怪了!還是一樣不能解決,明明用StringWriter是以string的方式組資料!!
結果,問題出於CType(dgData.Items(i).FindControl("txtContract_Content"), TextBox).Text雖然抓到了控制項,但....
少了.ToString()??
改成CType(dgData.Items(i).FindControl("txtContract_Content"), TextBox).Text.ToString()就好了!!
這.....太扯了~

熱門文章