新しいワークブックを作成し、最初の 10 個のセルに値を追加するには、次の手順に従います。
<Grid> </Grid> タグの間にカーソルを置きます。| XAML マークアップ |
コードのコピー
|
|---|---|
<Button x:Name="HelloButton" Content="Click Hello" /> |
|
| XAML マークアップ |
コードのコピー
|
|---|---|
<Button x:Name="SaveButton" Content="保存..." /> |
|
| XAML マークアップ |
コードのコピー
|
|---|---|
<TextBox Name="_tbContent" Text="空白" IsReadOnly="True" AcceptsReturn="True" FontFamily="Courier New" Background="White" Margin="465,10,242,722" /> |
|
| C# |
コードのコピー
|
|---|---|
using C1.Xaml.Excel; |
|
| C# |
コードのコピー
|
|---|---|
public sealed partial class MainPage : Page
{
C1XLBook _book;
}
|
|
| C# |
コードのコピー
|
|---|---|
_book = new C1XLBook(); |
|
| C# |
コードのコピー
|
|---|---|
void RefreshView()
{
}
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
// 手順1:新しいワークブックを作成します
_book = new C1XLBook();
// 手順2:デフォルトで作成されたシートを取得して名前を付けます
XLSheet sheet = _book.Sheets[0];
sheet.Name = "Hello World";
// 手順3:奇数と偶数のスタイルを作成します
XLStyle styleOdd = new XLStyle(_book);
styleOdd.Font = new XLFont("Tahoma", 9, false, true);
styleOdd.ForeColor = Color.FromArgb(255, 0, 0, 255);
XLStyle styleEven = new XLStyle(_book);
styleEven.Font = new XLFont("Tahoma", 9, true, false);
styleEven.ForeColor = Color.FromArgb(255, 255, 0, 0);
// 手順4:中身を書き込み、セルの書式を設定します
for (int i = 0; i < 100; i++)
{
XLCell cell = sheet[i, 0];
cell.Value = i + 1;
cell.Style = ((i + 1) % 2 == 0) ? styleEven : styleOdd;
}
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
async void SaveButton_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("Open XML Excel ファイル", new List() { ".xlsx" });
picker.FileTypeChoices.Add("BIFF Excel ファイル", new List() { ".xls" });
picker.SuggestedFileName ="新しいブック";
var file = await picker.PickSaveFileAsync();
if (file != null)
{
try
{
// 手順1:ファイルを保存します
var fileFormat = Path.GetExtension(file.Path).Equals(".xls") ? FileFormat.Biff8 : FileFormat.OpenXml;
await _book.SaveAsync(file, fileFormat);
// 手順2:ユーザーフィードバック
_tbContent.Text = string.Format("ファイルを保存しました : {0}.", file.Path);
RefreshView();
}
catch (Exception x)
{
_tbContent.Text = string.Format("例外 : {0}", x.Message);
}
}
}
|
|