// 新規ワークブックの作成 var workbook = new GrapeCity.Documents.Excel.Workbook(); IWorksheet worksheet = workbook.Worksheets[0]; // 列幅と行高を変更 int[] columnWidths = { 24, 13, 14, 17 }; int[] rowHeights = { 15, 9, 19, 17, 17, 35, 32, 7, 17, 15, 15, 15, 15, 15, 15, 15, 15, 17 }; for (int i = 0; i < columnWidths.Length; i++) { worksheet.Columns[i].ColumnWidth = columnWidths[i]; } for (int i = 0; i < rowHeights.Length; i++) { worksheet.Rows[i].RowHeight = rowHeights[i]; } // データおよびスタイルを設定 worksheet.Range["A3"].Value = "請求書"; worksheet.Range["A3"].Font.Size = 14; worksheet.Range["A3"].Font.Bold = true; worksheet.Range["C3"].Value = "日付:"; worksheet.Range["C3"].HorizontalAlignment = HorizontalAlignment.Right; worksheet.Range["D3"].Formula2 = "=NOW()"; worksheet.Range["D3"].NumberFormat = "yyyy/m/d"; worksheet.Range["D3"].Borders[BordersIndex.EdgeBottom].LineStyle = BorderLineStyle.Medium; worksheet.Range["A5"].Value = "送付先住所:"; worksheet.Range["C5"].Value = "請求先住所:"; worksheet.Range["A6"].Value = "埼玉県川口市\n本町4-1-8\n川口センタービル 3F"; worksheet.Range["C6"].Value = "宮城県仙台市泉区\n紫山3-1-4"; // テーブルのデータを設定 object[,] data = new object[,] { {"製品", "単価", "数量", "合計価格" }, {"自転車", 12500, 1, "" }, {"ヘルメット", 3500, 1, "" }, {"手袋", 1500, 1, "" }, {"付属品", 2000, 3, "" }, }; worksheet.Range["A9:D13"].Value = data; // テーブルを追加 ITable table = worksheet.Tables.Add(worksheet.Range["A9:D13"], true); // テーブルのスタイルを設定 worksheet.Range["B10:B13"].NumberFormat = "_(¥* #,##0.00_);_(¥* (#,##0.00);_(¥* \" - \"??_);_(@_)"; worksheet.Range["D10:D13"].FormulaR1C1 = "=[@数量]*[@単価]"; worksheet.Range["D10:D13"].NumberFormat = "_(¥* #,##0.00_);_(¥* (#,##0.00);_(¥* \" - \"??_);_(@_)"; table.Columns[0].TotalsCalculation = TotalsCalculation.None; table.Columns[1].TotalsCalculation = TotalsCalculation.None; table.Columns[2].TotalsCalculation = TotalsCalculation.None; table.Columns[3].TotalsCalculation = TotalsCalculation.Sum; table.ShowTotals = true; // セルを結合 worksheet.Range["A3:B3"].Merge(); worksheet.Range["A5:B5"].Merge(); worksheet.Range["A5:B5"].Borders.LineStyle = BorderLineStyle.Medium; worksheet.Range["C5:D5"].Merge(); worksheet.Range["C5:D5"].Borders.LineStyle = BorderLineStyle.Medium; worksheet.Range["A6:B7"].Merge(); worksheet.Range["A6:B7"].WrapText = true; worksheet.Range["A6:B7"].VerticalAlignment = VerticalAlignment.Center; worksheet.Range["A6:B7"].Borders.LineStyle = BorderLineStyle.Medium; worksheet.Range["C6:D7"].Merge(); worksheet.Range["C6:D7"].WrapText = true; worksheet.Range["C6:D7"].VerticalAlignment = VerticalAlignment.Center; worksheet.Range["C6:D7"].Borders.LineStyle = BorderLineStyle.Medium; worksheet.SheetView.DisplayGridlines = false; // テーブルを範囲に変換 table.ConvertToRange(); // xlsx ファイルに保存 workbook.Save("ConvertToRange.xlsx");
' 新規ワークブックの作成 Dim workbook As New Workbook Dim worksheet As IWorksheet = workbook.Worksheets(0) ' 列幅と行高を変更 Dim columnWidths() = {24, 13, 14, 17} Dim rowHeights() = {15, 9, 19, 17, 17, 35, 32, 7, 17, 15, 15, 15, 15, 15, 15, 15, 15, 17} For i = 0 To columnWidths.Length - 1 worksheet.Columns(i).ColumnWidth = columnWidths(i) Next For i = 0 To rowHeights.Length - 1 worksheet.Rows(i).RowHeight = rowHeights(i) Next ' データおよびスタイルを設定 worksheet.Range("A3").Value = "請求書" worksheet.Range("A3").Font.Size = 14 worksheet.Range("A3").Font.Bold = True worksheet.Range("C3").Value = "日付:" worksheet.Range("C3").HorizontalAlignment = HorizontalAlignment.Right worksheet.Range("D3").Formula2 = "=NOW()" worksheet.Range("D3").NumberFormat = "yyyy/m/d" worksheet.Range("D3").Borders(BordersIndex.EdgeBottom).LineStyle = BorderLineStyle.Medium worksheet.Range("A5").Value = "送付先住所:" worksheet.Range("C5").Value = "請求先住所:" worksheet.Range("A6").Value = "埼玉県川口市" & vbCrLf & "本町4-1-8" & vbCrLf & "川口センタービル 3F" worksheet.Range("C6").Value = "宮城県仙台市泉区" & vbCrLf & "紫山3-1-4" ' テーブルのデータを設定 Dim data(,) As Object = { {"製品", "単価", "数量", "合計価格"}, {"自転車", 12500, 1, ""}, {"ヘルメット", 3500, 1, ""}, {"手袋", 1500, 1, ""}, {"付属品", 2000, 3, ""} } worksheet.Range("A9:D13").Value = data ' テーブルを追加 Dim table As ITable = worksheet.Tables.Add(worksheet.Range("A9:D13"), True) ' テーブルのスタイルを設定 worksheet.Range("B10:B13").NumberFormat = "_(¥* #,##0.00_);_(¥* (#,##0.00);_(¥* "" - ""??_);_(@_)" worksheet.Range("D10:D13").FormulaR1C1 = "=[@数量]*[@単価]" worksheet.Range("D10:D13").NumberFormat = "_(¥* #,##0.00_);_(¥* (#,##0.00);_(¥* "" - ""??_);_(@_)" table.Columns(0).TotalsCalculation = TotalsCalculation.None table.Columns(1).TotalsCalculation = TotalsCalculation.None table.Columns(2).TotalsCalculation = TotalsCalculation.None table.Columns(3).TotalsCalculation = TotalsCalculation.Sum table.ShowTotals = True ' セルを結合 worksheet.Range("A3:B3").Merge() worksheet.Range("A5:B5").Merge() worksheet.Range("A5:B5").Borders.LineStyle = BorderLineStyle.Medium worksheet.Range("C5:D5").Merge() worksheet.Range("C5:D5").Borders.LineStyle = BorderLineStyle.Medium worksheet.Range("A6:B7").Merge() worksheet.Range("A6:B7").WrapText = True worksheet.Range("A6:B7").VerticalAlignment = VerticalAlignment.Center worksheet.Range("A6:B7").Borders.LineStyle = BorderLineStyle.Medium worksheet.Range("C6:D7").Merge() worksheet.Range("C6:D7").WrapText = True worksheet.Range("C6:D7").VerticalAlignment = VerticalAlignment.Center worksheet.Range("C6:D7").Borders.LineStyle = BorderLineStyle.Medium worksheet.SheetView.DisplayGridlines = False ' テーブルを範囲に変換 table.ConvertToRange() ' xlsx ファイルに保存 workbook.Save("ConvertToRange.xlsx")