You can easily add page breaks in rows and columns for files in OpenXML (.xlsx) format using the PageBreak and PageBreak properties.
1. Add a reference to C1.Silverlight.Excel.dlland create a C1XLBook.
2. Add some text values and page breaks.
3. Save the workbook. The code looks like the following. In this example it is placed within a button1_Click event so the Save As dialog box will open when the user clicks the button.
•C#
private void button1_Click_1(object sender, RoutedEventArgs e)
{
// create a new workbook to be saved
SaveBook(book =>
{
// add text values and page breaks
book.Sheets[0][2, 3].Value = "page1";
book.Sheets[0].Rows[2].PageBreak = true;
book.Sheets[0][0, 1].Value = "test1";
book.Sheets[0][0, 2].Value = "test2";
book.Sheets[0].Columns[1].PageBreak = true;
book.Sheets[0][3, 3].Value = "page2";
});
}
// save the file
private void SaveBook(Action<C1XLBook> action)
{
var dlg = new SaveFileDialog();
dlg.Filter = "Excel Files (*.xlsx)|*.xlsx";
if (dlg.ShowDialog() == true)
{
try
{
var book = new C1XLBook();
if (action != null)
{
action(book);
}
using (var stream = dlg.OpenFile())
{
book.Save(stream);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
}
4. Run the project to save the .xlsx file and then open the file.
5. In Excel, select the Page Layout tab, and select the Print checkbox under Gridlines. The worksheet should look similar to the following: