2017-11-27 17 views
0

Visual Studioで開発しているUWPアプリケーションがあります。それは正常にAPI呼び出しを行い、データを返します。これまでは、このデータをグリッド表示に限定して表示していました。しかし、私はこのデータをExcel文書にエクスポートする "保存"機能を追加したいと思います。リストデータをUWPのExcelドキュメントにエクスポート

どのように私はこれについて行くことができますか?

ご協力いただきまして誠にありがとうございます。

+0

データグリッドやデータテーブルを使用している場合は文字通りコードを作成し、1行のコードでラムダ式を使用し、そのデータを.txtまたは.csvファイルにエクスポートすると、特に多くの例がありますスタックオーバーフローで – MethodMan

答えて

0

私が直接エクセルを使用しないように、あなたに助言が、異なるアプローチを使用することになり、この

private void button_SAVE(object sender, EventArgs e) 
{ 
    // creating Excel Application 
    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
    // creating new WorkBook within Excel application 
    Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
    // creating new Excelsheet in workbook 
    Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
    // see the excel sheet behind the program 
    app.Visible = true; 
    // get the reference of first sheet. By default its name is Sheet1. 
    // store its reference to worksheet 
    worksheet = workbook.Sheets["Sheet1"]; 
    worksheet = workbook.ActiveSheet; 
    // changing the name of active sheet 
    worksheet.Name = "Exported from gridview"; 
    // storing header part in Excel 
    for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) 
    { 
     worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText; 
    } 
    // storing Each row and column value to excel sheet 
    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { 
    for (int j = 0; j < dataGridView1.Columns.Count; j++) { 
     worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); 
    } 
} 
// save the application 
workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
// Exit from the application 
app.Quit(); 
0

を試してみてください。あなたのために仕事をするためのナゲットパッケージを手に入れてみてください(Excelなどを扱う)。エクセル自体(interopライブラリとして)は、諺の痛みですが、頬です。

良い1:https://www.nuget.org/packages/EPPlus/

あなたはまだあなたがあなたのグリッドの取得に同じデータを使用しますが、エクセルにエクスポートすることができます。上記のEPPLusには多くの例がある。

+0

https://github.com/JanKallman/EPPlus github source – Leon

0

このpostのSunteenの回答を参照してください。これを行うライブラリがあります(例doc

自分でcsvを作成する場合は、CSVの解析についてblogがあります。

関連する問題