下記のサンプルコードは、ワークブックの書式設定方法の例となります。
Visual Basic コードの書き方
Visual Basic |
コードのコピー
|
---|---|
Private Sub Form1_Load(sender As Object, e As EventArgs) Dim book As New C1XLBook() Dim sheet As XLSheet = book.Sheets(0) Dim totalStyle As New XLStyle(book) totalStyle.Font = New Font(book.DefaultFont, FontStyle.Bold) sheet(2, 1).Value = "Number" sheet(2, 2).Value = "ID" sheet(3, 1).Value = 12 sheet(3, 2).Value = 17 sheet.Rows(3).OutlineLevel = 2 sheet.Rows(3).Visible = False sheet(4, 1).Value = 12 sheet(4, 2).Value = 14 sheet.Rows(4).OutlineLevel = 2 sheet.Rows(4).Visible = False sheet(5, 1).Value = "12 Total" sheet(5, 1).Style = totalStyle sheet(5, 2).Value = 31 sheet(5, 2).Formula = "SUBTOTAL(9,C4:C5)" sheet.Rows(5).OutlineLevel = 1 sheet(6, 1).Value = 34 sheet(6, 2).Value = 109 sheet.Rows(6).OutlineLevel = 2 sheet(7, 1).Value = "34 Total" sheet(7, 1).Style = totalStyle sheet(7, 2).Value = 109 sheet(7, 2).Formula = "SUBTOTAL(9,C7:C7)" sheet.Rows(7).OutlineLevel = 1 sheet(8, 1).Value = "Grand Total" sheet(8, 1).Style = totalStyle sheet(8, 2).Value = 140 sheet(8, 2).Formula = "SUBTOTAL(9,C4:C7)" sheet.Rows(8).OutlineLevel = 0 book.Save("c:\mybook.xls") System.Diagnostics.Process.Start("C:\mybook.xls") End Sub |
C# コードの書き方
C# |
コードのコピー
|
---|---|
private void Form1_Load(object sender, EventArgs e) { C1XLBook book = new C1XLBook(); XLSheet sheet = book.Sheets[0]; XLStyle totalStyle = new XLStyle(book); totalStyle.Font = new Font(book.DefaultFont, FontStyle.Bold); sheet[2, 1].Value = "Number"; sheet[2, 2].Value = "ID"; sheet[3, 1].Value = 12; sheet[3, 2].Value = 17; sheet.Rows[3].OutlineLevel = 2; sheet.Rows[3].Visible = false; sheet[4, 1].Value = 12; sheet[4, 2].Value = 14; sheet.Rows[4].OutlineLevel = 2; sheet.Rows[4].Visible = false; sheet[5, 1].Value = "12 Total"; sheet[5, 1].Style = totalStyle; sheet[5, 2].Value = 31; sheet[5, 2].Formula = "SUBTOTAL(9,C4:C5)"; sheet.Rows[5].OutlineLevel = 1; sheet[6, 1].Value = 34; sheet[6, 2].Value = 109; sheet.Rows[6].OutlineLevel = 2; sheet[7, 1].Value = "34 Total"; sheet[7, 1].Style = totalStyle; sheet[7, 2].Value = 109; sheet[7, 2].Formula = "SUBTOTAL(9,C7:C7)"; sheet.Rows[7].OutlineLevel = 1; sheet[8, 1].Value = "Grand Total"; sheet[8, 1].Style = totalStyle; sheet[8, 2].Value = 140; sheet[8, 2].Formula = "SUBTOTAL(9,C4:C7)"; sheet.Rows[8].OutlineLevel = 0; book.Save(@"c:\mybook.xls"); System.Diagnostics.Process.Start(@"C:\mybook.xls"); } |
SUBTOTAL の数式で指定された行の合計が取得されます。