To create a new XLSX file using Excel for UWP, three steps are required:
The code starts by creating a new workbook, and then getting the XLSheet that is created automatically. The third step creates the styles that will be applied to even and odd values before content is written and cells are formatted.
The indexer in the XLSheet object automatically creates cells, if necessary. This makes it easy to fill worksheets that you create. If you want to find out the sheet dimensions, use the sheet's Rows.Count and Columns.Count properties.
The style applied in step three creates a sheet where even numbers are shown in bold red characters and odd numbers are shown in italic blue.
C# Copy Code private void HelloButton_Click(object sender, RoutedEventArgs e) { // step 1: create a new workbook _book = new C1XLBook(); // step 2: get the sheet that was created by default, give it a name XLSheet sheet = _book.Sheets[0]; sheet.Name = "Hello World"; // step 3: create styles for odd and even values 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); // step 4: write content and format into some cells for (int i = 0; i < 100; i++) { XLCell cell = sheet[i, 0]; cell.Value = i + 1; cell.Style = ((i + 1) % 2 == 0) ? styleEven : styleOdd; } // step 5: allow user to save the file _tbContent.Text = "'Hello World' workbook has been created, you can save it now."; RefreshView(); }