如何使用C#在Excel工作表中加粗特定行或单元格的字体?

我将数据从List <>导出为Excel。 我想做一些特定的行和单元格粗体。 我也需要合并一些单元格。

以下是我正在使用的代码。

try { Excel.Application application; Excel.Workbook workBook; Excel.Worksheet workSheet; object misValue = System.Reflection.Missing.Value; application = new Excel.ApplicationClass(); workBook = application.Workbooks.Add(misValue); workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1); int i = 1; workSheet.Cells[i, 2] = "MSS Close Sheet"; i++; workSheet.Cells[i, 2] = "MSS - " + dpsNoTextBox.Text; i++; workSheet.Cells[i, 2] = customerNameTextBox.Text; i++; workSheet.Cells[i, 2] = "Opening Date : "; workSheet.Cells[i, 3] = openingDateTextBox.Value.ToShortDateString(); i++; workSheet.Cells[i, 2] = "Closing Date : "; workSheet.Cells[i, 3] = closingDateTextBox.Value.ToShortDateString(); i++; i++; i++; workSheet.Cells[i, 1] = "SL. No"; workSheet.Cells[i, 2] = "Month"; workSheet.Cells[i, 3] = "Amount Deposited"; workSheet.Cells[i, 4] = "Fine"; workSheet.Cells[i, 5] = "Cumulative Total"; workSheet.Cells[i, 6] = "Profit + Cumulative Total"; workSheet.Cells[i, 7] = "Profit @ " + profitRateComboBox.Text; i++; ///////////////////////////////////////////////////////// foreach (RecurringDeposit rd in RecurringDepositList) { workSheet.Cells[i, 1] = rd.SN.ToString(); workSheet.Cells[i, 2] = rd.MonthYear; workSheet.Cells[i, 3] = rd.InstallmentSize.ToString(); workSheet.Cells[i, 4] = ""; workSheet.Cells[i, 5] = rd.CumulativeTotal.ToString(); workSheet.Cells[i, 6] = rd.ProfitCumulative.ToString(); workSheet.Cells[i, 7] = rd.Profit.ToString(); i++; } ////////////////////////////////////////////////////// //////////////////////////////////////////////////////// workSheet.Cells[i, 2] = "Total (" + RecurringDepositList.Count + " months installment)"; workSheet.Cells[i, 3] = totalAmountDepositedTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "a) Total Amount Deposited"; workSheet.Cells[i, 3] = totalAmountDepositedTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "b) Fine"; workSheet.Cells[i, 3] = ""; i++; workSheet.Cells[i, 2] = "c) Total Pft Paid"; workSheet.Cells[i, 3] = totalProfitPaidTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "Sub Total"; workSheet.Cells[i, 3] = (totalAmountDepositedTextBox.Value + totalProfitPaidTextBox.Value).ToString("0.00"); i++; workSheet.Cells[i, 2] = "Deduction"; i++; workSheet.Cells[i, 2] = "a) Excise Duty"; workSheet.Cells[i, 3] = "0"; i++; workSheet.Cells[i, 2] = "b) Income Tax on Pft. @ " + incomeTaxPercentageTextBox.Text; workSheet.Cells[i, 3] = "0"; i++; workSheet.Cells[i, 2] = "c) Account Closing Charge "; workSheet.Cells[i, 3] = closingChargeCommaNumberTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "d) Outstanding on BAIM(FO) "; workSheet.Cells[i, 3] = baimFOLowerTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "Total Deduction "; workSheet.Cells[i, 3] = (incomeTaxDeductionTextBox.Value + closingChargeCommaNumberTextBox.Value + baimFOTextBox.Value).ToString("0.00"); i++; workSheet.Cells[i, 2] = "Client Paid "; workSheet.Cells[i, 3] = customerPayableNumberTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "e) Current Balance "; workSheet.Cells[i, 3] = currentBalanceCommaNumberTextBox.Value.ToString("0.00"); workSheet.Cells[i, 5] = "Exp. Pft paid on MSS A/C(PL67054)"; workSheet.Cells[i, 6] = plTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "e) Total Paid "; workSheet.Cells[i, 3] = customerPayableNumberTextBox.Value.ToString("0.00"); workSheet.Cells[i, 5] = "IT on Pft (BDT16216)"; workSheet.Cells[i, 6] = incomeTaxDeductionTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "Difference"; workSheet.Cells[i, 3] = (currentBalanceCommaNumberTextBox.Value - customerPayableNumberTextBox.Value).ToString("0.00"); workSheet.Cells[i, 5] = "Account Closing Charge"; workSheet.Cells[i, 6] = closingChargeCommaNumberTextBox.Value; i++; /////////////////////////////////////////////////////////////// workBook.SaveAs("D:\\" + dpsNoTextBox.Text.Trim() + "-" + customerNameTextBox.Text.Trim() + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); workBook.Close(true, misValue, misValue); application.Quit(); releaseObject(workSheet); releaseObject(workBook); releaseObject(application); 

该表应该是这样的:

如何加粗整行10例子:

 workSheet.Cells[10, 1].EntireRow.Font.Bold = true; 

更正式地说:

 Microsoft.Office.Interop.Excel.Range rng = workSheet.Cells[10, 1] as Xl.Range; rng.EntireRow.Font.Bold = true; 

如何加粗特定单元格'A10'例如:

 workSheet.Cells[10, 1].Font.Bold = true; 

更正式一些:

 int row = 1; int column = 1; /// 1 = 'A' in Excel Microsoft.Office.Interop.Excel.Range rng = workSheet.Cells[row, column] as Xl.Range; rng.Font.Bold = true; 

你的问题是有点不清楚…因为你指出你想要在Excel中加粗的部分是从Word方法导入的DataGridView。 你可能想要大胆的Excel文档的第一行?

 using xl = Microsoft.Office.Interop.Excel; xl.Range rng = (xl.Range)xlWorkSheet.Rows[0]; rng.Font.Bold = true; 

就那么简单!

HTH,Z

很久以前,我在一个项目中做了这个。 下面给出的代码用特定的列名写入整行粗体,所有这些列都以粗体格式写入。

 private void WriteColumnHeaders(DataColumnCollection columnCollection, int row, int column) { // row represent particular row you want to bold its content. for (i = 0; i < columnCollection.Count; i++) { DataColumn col = columnCollection[i]; xlWorkSheet.Cells[row, column + i + 1] = col.Caption; // Some Font Styles xlWorkSheet.Cells[row, column + i + 1].Style.Font.Bold = true; xlWorkSheet.Cells[row, column + i + 1].Interior.Color = Color.FromArgb(192, 192, 192); //xlWorkSheet.Columns[i + 1].ColumnWidth = xlWorkSheet.Columns[i+1].ColumnWidth + 10; } } 

您必须传递行0的值,以便您的Excel表的第一行具有粗体字大小的列标题。 只需将DataColumnCollection更改为您的列名称,然后将col.Caption更改为特定的列名称即可。

备用

你可以这样做,你想要粗体的Excel表单元。

 xlWorkSheet.Cells[row, column].Style.Font.Bold = true; 

这对我有用,所以试试吧:

 Microsoft.Office.Interop.Excel.Range rng =(Microsoft.Office.Interop.Excel.Range)XcelApp.Cells[1, i]; rng.Font.Bold = true; rng.Interior.Color =System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); rng.BorderAround(); 

以下是您所需的确切代码,使您的工作表看起来完全如所附的PDF中所示:

 try { Excel.Application application; Excel.Workbook workBook; Excel.Worksheet workSheet; object misValue = System.Reflection.Missing.Value; application = new Excel.ApplicationClass(); workBook = application.Workbooks.Add(misValue); workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(1); int i = 1; workSheet.Cells[i, 2] = "MSS Close Sheet"; WorkSheet.Cells[i, 2].Style.Font.Bold = true; i++; workSheet.Cells[i, 2] = "MSS - " + dpsNoTextBox.Text; WorkSheet.Cells[i, 2].Style.Font.Bold = true; i++; workSheet.Cells[i, 2] = customerNameTextBox.Text; i++; workSheet.Cells[i, 2] = "Opening Date : "; workSheet.Cells[i, 3] = openingDateTextBox.Value.ToShortDateString(); i++; workSheet.Cells[i, 2] = "Closing Date : "; workSheet.Cells[i, 3] = closingDateTextBox.Value.ToShortDateString(); i++; i++; i++; workSheet.Cells[i, 1] = "SL. No"; workSheet.Cells[i, 2] = "Month"; workSheet.Cells[i, 3] = "Amount Deposited"; workSheet.Cells[i, 4] = "Fine"; workSheet.Cells[i, 5] = "Cumulative Total"; workSheet.Cells[i, 6] = "Profit + Cumulative Total"; workSheet.Cells[i, 7] = "Profit @ " + profitRateComboBox.Text; WorkSheet.Cells[i, 1].EntireRow.Font.Bold = true; i++; ///////////////////////////////////////////////////////// foreach (RecurringDeposit rd in RecurringDepositList) { workSheet.Cells[i, 1] = rd.SN.ToString(); workSheet.Cells[i, 2] = rd.MonthYear; workSheet.Cells[i, 3] = rd.InstallmentSize.ToString(); workSheet.Cells[i, 4] = ""; workSheet.Cells[i, 5] = rd.CumulativeTotal.ToString(); workSheet.Cells[i, 6] = rd.ProfitCumulative.ToString(); workSheet.Cells[i, 7] = rd.Profit.ToString(); i++; } ////////////////////////////////////////////////////// //////////////////////////////////////////////////////// workSheet.Cells[i, 2] = "Total (" + RecurringDepositList.Count + " months installment)"; WorkSheet.Cells[i, 2].Style.Font.Bold = true; workSheet.Cells[i, 3] = totalAmountDepositedTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "a) Total Amount Deposited"; workSheet.Cells[i, 3] = totalAmountDepositedTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "b) Fine"; workSheet.Cells[i, 3] = ""; i++; workSheet.Cells[i, 2] = "c) Total Pft Paid"; workSheet.Cells[i, 3] = totalProfitPaidTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "Sub Total"; WorkSheet.Cells[i, 2].Style.Font.Bold = true; workSheet.Cells[i, 3] = (totalAmountDepositedTextBox.Value + totalProfitPaidTextBox.Value).ToString("0.00"); i++; workSheet.Cells[i, 2] = "Deduction"; WorkSheet.Cells[i, 2].Style.Font.Bold = true; i++; workSheet.Cells[i, 2] = "a) Excise Duty"; workSheet.Cells[i, 3] = "0"; i++; workSheet.Cells[i, 2] = "b) Income Tax on Pft. @ " + incomeTaxPercentageTextBox.Text; workSheet.Cells[i, 3] = "0"; i++; workSheet.Cells[i, 2] = "c) Account Closing Charge "; workSheet.Cells[i, 3] = closingChargeCommaNumberTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "d) Outstanding on BAIM(FO) "; workSheet.Cells[i, 3] = baimFOLowerTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "Total Deduction "; WorkSheet.Cells[i, 2].Style.Font.Bold = true; workSheet.Cells[i, 3] = (incomeTaxDeductionTextBox.Value + closingChargeCommaNumberTextBox.Value + baimFOTextBox.Value).ToString("0.00"); i++; workSheet.Cells[i, 2] = "Client Paid "; WorkSheet.Cells[i, 2].Style.Font.Bold = true; workSheet.Cells[i, 3] = customerPayableNumberTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "e) Current Balance "; workSheet.Cells[i, 3] = currentBalanceCommaNumberTextBox.Value.ToString("0.00"); workSheet.Cells[i, 5] = "Exp. Pft paid on MSS A/C(PL67054)"; workSheet.Cells[i, 6] = plTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "e) Total Paid "; workSheet.Cells[i, 3] = customerPayableNumberTextBox.Value.ToString("0.00"); workSheet.Cells[i, 5] = "IT on Pft (BDT16216)"; workSheet.Cells[i, 6] = incomeTaxDeductionTextBox.Value.ToString("0.00"); i++; workSheet.Cells[i, 2] = "Difference"; WorkSheet.Cells[i, 2].Style.Font.Bold = true; workSheet.Cells[i, 3] = (currentBalanceCommaNumberTextBox.Value - customerPayableNumberTextBox.Value).ToString("0.00"); workSheet.Cells[i, 5] = "Account Closing Charge"; workSheet.Cells[i, 6] = closingChargeCommaNumberTextBox.Value; i++; /////////////////////////////////////////////////////////////// workBook.SaveAs("D:\\" + dpsNoTextBox.Text.Trim() + "-" + customerNameTextBox.Text.Trim() + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); workBook.Close(true, misValue, misValue); application.Quit(); releaseObject(workSheet); releaseObject(workBook); releaseObject(application);