async void _btnSave_Click(object sender, RoutedEventArgs e)
        {
            Debug.Assert(_book != null);

            var picker = new Windows.Storage.Pickers.FileSavePicker();

            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
            picker.FileTypeChoices.Add(Strings.Typexlsx, new List <string>()
            {
                ".xlsx"
            });
            picker.FileTypeChoices.Add(Strings.Typecsv, new List <string>()
            {
                ".csv"
            });
            picker.SuggestedFileName = Strings.DefaultFileName;

            var file = await picker.PickSaveFileAsync();

            if (file != null)
            {
                try
                {
                    // step 1: save file
                    var fileFormat = Path.GetExtension(file.Path).Equals(".csv") ? FileFormat.Csv : FileFormat.OpenXml;
                    await _book.SaveAsync(file, fileFormat);

                    // step 2: user feedback
                    _tbContent.Text = string.Format(Strings.SaveLocationTip, file.Path);

                    RefreshView();
                }
                catch (Exception x)
                {
                    _tbContent.Text = string.Format(Strings.SaveAndOpenException, x.Message);
                }
            }
        }
Example #2
0
        private async void ExcelButton_Click(object sender, RoutedEventArgs e)
        {
            // 現在、FlexGrid に表示されている順のデータ
            var currentData = this.flexgrid1.Rows.Select(r => r.DataItem).Cast <Book>();

            // Excel データの作成
            // https://docs.grapecity.com/help/c1/uwp/uwp_excel/#Step_2_of_4-_Adding_Content_to_a_C1XLBook.html

            // 新しい Excel ワークブックを作成
            var xlBook = new C1XLBook();

            // デフォルトで作成されたシートを取得
            XLSheet sheet = xlBook.Sheets[0];

            // シートの中身を書き込みます
            int rowIndex = 0;

            // ヘッダー行
            sheet[rowIndex, 0].Value = "書名";
            sheet[rowIndex, 1].Value = "ISBN";
            sheet[rowIndex, 2].Value = "バーコード";
            sheet.Columns[2].Width
                = C1XLBook.PixelsToTwips(this.HiddenBarCode.ActualWidth);
            sheet[rowIndex, 3].Value = "価格";
            // データ行
            foreach (var book in currentData)
            {
                rowIndex++;

                // バーコードの画像を作る
                this.HiddenBarCode.Text = book.IsbnWithoutCheckDigit;
                C1Bitmap bitmap = new C1Bitmap();
                using (var ms = new InMemoryRandomAccessStream().AsStream())
                {
                    await this.HiddenBarCode.SaveAsync(ms, ImageFormat.Png);

                    bitmap.Load(ms);
                }

                // 行の高さをバーコードの画像に合わせる
                sheet.Rows[rowIndex].Height
                    = C1XLBook.PixelsToTwips(this.HiddenBarCode.ActualHeight);

                // 1行分のデータとバーコード画像をセット
                sheet[rowIndex, 0].Value = book.Title;
                sheet[rowIndex, 1].Value = book.Isbn;
                sheet[rowIndex, 2].Value = bitmap;
                sheet[rowIndex, 3].Value = book.Price;
            }

            // Excel ファイルへの書き出し
            // https://docs.grapecity.com/help/c1/uwp/uwp_excel/#Step_3_of_4-_Saving_the_XLSX_File.html
            var picker = new FileSavePicker()
            {
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            };

            picker.FileTypeChoices.Add("Open XML Excel ファイル", new string[] { ".xlsx", });
            picker.FileTypeChoices.Add("BIFF Excel ファイル", new string[] { ".xls", });
            picker.SuggestedFileName = "BarCodeControlSample";
            var file = await picker.PickSaveFileAsync();

            if (file != null)
            {
                var fileFormat = Path.GetExtension(file.Path).Equals(".xls") ? FileFormat.OpenXmlTemplate : FileFormat.OpenXml;
                await xlBook.SaveAsync(file, fileFormat);
            }
        }