3

ボタンをクリックすると、flでユーザーのExcelファイルを生成する必要があります。以前はNetofficeを使用していましたが、デスクトップアプリケーションでは問題なく機能していました。しかし今、私はasp.netアプリで同じことをしたいと思っています。このように、私のサーバーコードはクライアントのExcelのコピーにアクセスできません。どのようなアプローチを取るべきですか?

4

5 に答える 5

8

EPPlusを使用します。サーバー上で Excel スプレッドシートを作成できます。私はそれを使用しましたが、うまくいきました。高度な機能をサポートします。

using (ExcelPackage pck = new ExcelPackage())
{
    //Create the worksheet
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
    ws.Cells["A1"].LoadFromDataTable(tbl, true);

    //Format the header for column 1-3
    using (ExcelRange rng = ws.Cells["A1:C1"])
    {
        rng.Style.Font.Bold = true;
        rng.Style.Fill.PatternType = ExcelFillStyle.Solid;

        //Set Pattern for the background to Solid
        rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));

        //Set color to dark blue
        rng.Style.Font.Color.SetColor(Color.White);
    }

    //Example how to Format Column 1 as numeric 
    using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
    {
        col.Style.Numberformat.Format = "#,##0.00";
        col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
    }

    //Write it back to the client
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                    
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.End();
}
于 2012-04-07T20:31:43.993 に答える
3

最も柔軟性があり、必要なことを正確に実行する可能性が高いのは、ある程度の作業が必要ですが、無料であり、実際に機能します。ツールキットを使用して既存のドキュメントを調べ、必要な機能を作成する方法を確認します。

XML2.0SDKを開く

于 2012-04-07T20:30:58.237 に答える
0

単純なHTMLテーブル(html、head、bodyタグを含む)を試すことができます。XLS拡張子で保存するだけです。


ここに画像の説明を入力してください

于 2012-04-07T20:29:26.420 に答える
0

DataGrid を使用して、その場で Excel ファイルを作成できます。エクセルは必要ありません。

public static void ExportDataSetToExcel(DataSet ds, string filename)
{
    HttpResponse response = HttpContext.Current.Response;

    // first let's clean up the response.object
    response.Clear();
    response.Charset = "";

    // set the response mime type for excel
    response.ContentType = "application/vnd.ms-excel";
    response.AddHeader(
        "Content-Disposition",
        "attachment;filename=\"" + filename + "\""
    );

   // 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);
            response.Write(sw.ToString());
            dg.Dispose();
            ds.Dispose();
            response.End();
       }
    }
}
于 2012-04-08T05:02:32.463 に答える
0

Netoffice は、実行マシンに MS Office を必要とします。あなたのサーバーにはそれがありますか?

于 2012-04-07T20:34:53.050 に答える