2012-01-13 13 views
0
//i'm using VS 2008, dll as Microsoft.Office.Interop.Excel 
//sample code.. 
Excel.Application xlApp; 
Excel.Workbook xlWorkBook; 
Excel.Worksheet xlWorkSheet; 

object misValue = System.Reflection.Missing.Value;     
xlApp = new Excel.ApplicationClass();     
xlWorkBook = xlApp.Workbooks.Add(misValue);//create new work book 
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

//CREATING ONE Rectangle shape 
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 47, 24, 500, 90); 
Microsoft.Office.Interop.Excel.Shape[] myShapes = new Microsoft.Office.Interop.Excel.Shape[1]; 
//adding textbox 
myShapes[0] = xlWorkSheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 75, 60, 70, 30); 
myShapes[0].TextFrame.Characters(misValue, misValue).Text = "Tracking"; 

//ブラウザからダウンロードオプションを使用してください。ここ1)memorystreamに書き出す方法... 2)ブラウザからExcelファイルをダウンロードする方法... Micosoft.Office.Interop.Excelとc#

context.Response.ContentType = "application/vnd.ms-excel"; 
context.Response.AddHeader("Content-Disposition", "attachment; filename= report.xls"); 

//私は

string file = context.Server.MapPath("new.txt"); 
Byte[] myByte = File.ReadAllBytes(file); 

    context.Response.Clear(); 
    context.Response.BinaryWrite(myByte); 
    context.Response.Flush(); 
    context.Response.Close(); 

たちはMemoryStreamを内のオブジェクトを保存するためには、IPersistStreamインターフェイスを使用することができます..私は一時ファイルを使用せずに必要file.but一時的なテキストを使用していますか?

http://www.eggheadcafe.com/community/csharp/2/10101323/how-to-implement-ipersiststream-interface-in-c.aspx

+4

をしないでくださいこの! Office Automationライブラリは、デスクトップアプリケーションの内部で使用するように設計されています。 ASP.NETアプリケーションからそれらを使用しようとすると、さまざまな方法で失敗します。 –

+0

なぜCSVにエクスポートしないのですか? – doogle

+0

@ani:ASP.NETの期間は使用しないでください。 –

答えて

0

私は完全に、ジョン・サンダースに同意していますが、この方法を試してみてくださいかもしれません。

データセットをGridViewにバインドし、ダウンロードしてExcelにダウンロードします。コードのセグメント以下

Nowを使用して、GridViewのからデータをダウンロードするメソッドを作成して制御

private static void PrepareControlForExport(Control control) 
    { 
     for (int i = 0; i < control.Controls.Count; i++) 
     { 
      Control current = control.Controls[i]; 
      if (current is LinkButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 
      } 
      else if (current is ImageButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 
      } 
      else if (current is HyperLink) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 
      } 
      else if (current is DropDownList) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 
      } 
      else if (current is CheckBox) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 
      } 

      if (current.HasControls()) 
      { 
       PrepareControlForExport(current); 
      } 
     } 
    } 

から値を取得します。

private void ExportX() 
    { 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; Filename = ExcelReport.xls")); 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     HttpContext.Current.Response.ContentType = "application/ms-excel"; 
     StringWriter stringWrite = new StringWriter(); 
     HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWrite); 
     Table table = new Table(); 

     // include the gridline settings 
     table.GridLines = GridView1.GridLines; 
     if (GridView1.HeaderRow != null) 
     { 
      PrepareControlForExport(GridView1.HeaderRow); 
      table.Rows.Add(GridView1.HeaderRow); 
     } 
     foreach (GridViewRow row in GridView1.Rows) 
     { 
      PrepareControlForExport(row); 
      table.Rows.Add(row); 
     } 
     if (GridView1.FooterRow != null) 
     { 
      PrepareControlForExport(GridView1.HeaderRow); 
      table.Rows.Add(GridView1.FooterRow); 
     } 

     table.RenderControl(htmlWriter); 
     Response.Write(stringWrite.ToString()); 
     Response.End(); 
    } 



public override void VerifyRenderingInServerForm(Control control) 
    { 

    } 

上記の方法でデータをダウンロードしてください。

1

あなたはEPPlusを見てみたいことがあり行うことができますGridViewのより多くのフォーマットでExcelドキュメントを作成することに関心を持っている場合:

http://epplus.codeplex.com/

ライブラリがでExcelファイルを作成することが可能ですセル、フォーマットなどをカスタマイズする機能。

GridViewはあなたのために働きますが、そうでなければ他のExcelオプションがあります。コンテンツをメモリストリームに保存し、メモリストリームをブラウザに出力することができます。

ここでは、ASP.NET MVCで使用したFileResultハンドラのサンプルを示します。この概念はWebFormsに適用することができ、応答ストリームに書き出すだけです。私のクラスは、(this.dataTable)をロードするデータテーブルのプロパティを持っており、あなたはブラウザ(fileNameに)として、ファイルをダウンロードしたいファイル名と作業帳の名前(this.workBookName)

protected override void WriteFile(HttpResponseBase response) 
    { 
     using (ExcelPackage pck = new ExcelPackage()) 
     { 

      ExcelWorksheet ws = pck.Workbook.Worksheets.Add(this.workBookName); 

      ws.Cells["A1"].LoadFromDataTable(this.dataTable, true); 

      response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
      response.BinaryWrite(pck.GetAsByteArray()); 

     } 
    } 
関連する問題