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);
This question is related to
c#
windows
excel
datagridview
Your question is a little unclear...as the part that you indicate you want to bold in Excel is a DataGridView in the import from word method. Do you maybe want to bold the first row in the excel document?
using xl = Microsoft.Office.Interop.Excel;
xl.Range rng = (xl.Range)xlWorkSheet.Rows[0];
rng.Font.Bold = true;
Simple as that!
HTH, Z
Below is the exact code you need to make your sheet look exactly as it is in the attached 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);
this works for me, so try it :
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();
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;
I have done this in a project a long time ago. The code given below write a whole rows bold with specific column names and all of these columns are written in bold format.
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;
}
}
You must pass value of row 0 so that first row of your excel sheets have column headers with bold font size. Just change DataColumnCollection
to your columns name and change col.Caption
to specific column name.
Alternate
You may do this to cell of excel sheet you want bold.
xlWorkSheet.Cells[row, column].Style.Font.Bold = true;
Source: Stackoverflow.com