3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UWPアプリ開発メモ

Last updated at Posted at 2018-12-16

UWPアプリ開発メモ


UWPアプリを作ろうといろいろと試したときのメモです。


UWPアプリでのClosedXMLの使用

UWPアプリでClosedXMLが使えない。System.Drawingがサポートされていないとか。
UWPアプリでExcelファイルの読み込みをしたいので、.NET Standard LibraryでClosedXMLを使用し、そのLibraryをUWPプロジェクトから参照したら動作しました。Visual Studioからのデバッグ実行でしか確かめていませんが。

以下は、UWPアプリ画面(MainPage.xaml.cs)に実装した内容

MainPage.xaml.cs
var filePicker = new Windows.Storage.Pickers.FileOpenPicker();
filePicker.FileTypeFilter.Add(".xlsx");

StorageFile file = await filePicker.PickSingleFileAsync();
if (file != null)
{
    IBuffer buf = await FileIO.ReadBufferAsync(file);
    byte[] fileBinary = new byte[buf.Length];
    using (DataReader reader = DataReader.FromBuffer(buf))
    {
        reader.ReadBytes(fileBinary);

        using (MemoryStream ms = new MemoryStream(fileBinary))
        {
            List<string[]> lst = SampleExcelAccessor.ReadExcel(ms, );

            //~~ 取得したデータを処理 ~~
        }
    }
}

以下は、.NET Standard Libraryとして実装した内容

SampleExcelAccessor.cs
public class SampleExcelAccessor
{
    public static List<string[]> ReadExcel(Stream fileStream, string sheetName)
    {
        List<string[]> retData = new List<string[]>();

        XLWorkbook workbook = new XLWorkbook(fileStream);

        IXLWorksheet worksheet = workbook.Worksheet(sheetName);
        int lastRow = worksheet.LastRowUsed().RowNumber();
        int lastCol = worksheet.LastColumnUsed().ColumnNumber();

        for (int i = 1; i <= lastRow; i++)
        {
            List<string> rowData = new List<string>();
            for (int j = 1; j <= lastCol; j++)
            {
                IXLCell cell = worksheet.Cell(i, j);
                rowData.Add(cell.Value.ToString());
            }
            retData.Add(rowData.ToArray());
        }

        return retData;
    }
}

UWPアプリ画面のサイズ変更

こんな感じに実装すると画面サイズを設定できます。
下のサンプルは400×300の画面になります。

MainPage.xaml.cs
namespace App1
{
    /// <summary>
    /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            ApplicationView.PreferredLaunchViewSize
              = new Size { Height = 300, Width = 400 };
            ApplicationView.PreferredLaunchWindowingMode
              = ApplicationViewWindowingMode.PreferredLaunchViewSize;
        }
    }
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?