如何使用C#加粗Excel工作表中特定行或单元格的字体? [英] How can I bold the fonts of a specific row or cell in an Excel worksheet with C#?

查看:170
本文介绍了如何使用C#加粗Excel工作表中特定行或单元格的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据从列表导出为ex​​cel。我想使一些特定的行和单元格粗体。我也需要做一些细胞合并。



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

  try 
{
Excel.Application application;
Excel.Workbook工作簿;
Excel.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关闭表;
i ++;
workSheet.Cells [i,2] =MSS - + dpsNoTextBox.Text;
i ++;
workSheet.Cells [i,2] = customerNameTextBox.Text;
i ++;
workSheet.Cells [i,2] =开放日期:;
workSheet.Cells [i,3] = openingDateTextBox.Value.ToShortDateString();
i ++;
workSheet.Cells [i,2] =结束日期:;
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] =已存金额;
workSheet.Cells [i,4] =Fine;
workSheet.Cells [i,5] =累计总计;
workSheet.Cells [i,6] =利润+累计合计;
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 +月份分期付款);
workSheet.Cells [i,3] = totalAmountDepositedTextBox.Value.ToString(0.00);
i ++;

workSheet.Cells [i,2] =a)存款总金额;
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] =扣除;
i ++;

workSheet.Cells [i,2] =a)消费税;
workSheet.Cells [i,3] =0;
i ++;

workSheet.Cells [i,2] =b)Pft。@+ incomeTaxPercentageTextBox.Text;
workSheet.Cells [i,3] =0;
i ++;

workSheet.Cells [i,2] =c)帐户结算费;
workSheet.Cells [i,3] = closingChargeCommaNumberTextBox.Value.ToString(0.00);
i ++;

workSheet.Cells [i,2] =d)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] =客户付款;
workSheet.Cells [i,3] = customerPayableNumberTextBox.Value.ToString(0.00);
i ++;

workSheet.Cells [i,2] =e)当前余额;
workSheet.Cells [i,3] = currentBalanceCommaNumberTextBox.Value.ToString(0.00);
workSheet.Cells [i,5] =Exp。Pft支付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] =差异;
workSheet.Cells [i,3] =(currentBalanceCommaNumberTextBox.Value - customerPayableNumberTextBox.Value).ToString(0.00);
workSheet.Cells [i,5] =账户结算费;
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 =真正; 






更正式地:

  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 = Excel中的'A'

Microsoft.Office.Interop.Excel.Range rng = workSheet.Cells [row,column] as Xl.Range;
rng.Font.Bold = true;


I am exporting data from a List<> to excel. I want to make some specific rows and cells bold. I need to make some cell merged also.

Below is the code I am using.

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);

The sheet should look like this:

解决方案

How to Bold entire row 10 example:

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


More formally:

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


How to Bold Specific Cell 'A10' for example:

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


Little more formal:

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;

这篇关于如何使用C#加粗Excel工作表中特定行或单元格的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆