J'exporte des données d'une liste <> vers Excel. Je souhaite mettre en gras certaines lignes et cellules spécifiques. Je dois également fusionner certaines cellules.
Voici le code que j'utilise.
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);
Comment Gras exemple entier de la ligne 10:
workSheet.Cells[10, 1].EntireRow.Font.Bold = true;
Plus formellement:
Microsoft.Office.Interop.Excel.Range rng = workSheet.Cells[10, 1] as Xl.Range;
rng.EntireRow.Font.Bold = true;
Comment Gras Cellule spécifique 'A10' par exemple:
workSheet.Cells[10, 1].Font.Bold = true;
Un peu plus formel:
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;
Votre question n'est pas claire ... car la partie que vous indiquez que vous souhaitez mettre en gras dans Excel est un DataGridView dans la méthode d'importation à partir de Word. Voulez-vous peut-être mettre en gras la première ligne du document Excel?
using xl = Microsoft.Office.Interop.Excel;
xl.Range rng = (xl.Range)xlWorkSheet.Rows[0];
rng.Font.Bold = true;
Aussi simple que cela!
HTH, Z
Je l'ai fait dans un projet il y a longtemps. Le code ci-dessous écrit des lignes entières en gras avec des noms de colonnes spécifiques et toutes ces colonnes sont écrites en gras.
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;
}
}
Vous devez transmettre la valeur de la ligne 0 pour que la première ligne de vos feuilles Excel ait des en-têtes de colonne avec une taille de police en gras. Changez simplement DataColumnCollection
en nom de colonne et changez col.Caption
au nom de colonne spécifique.
Alternatif
Vous pouvez le faire sur la cellule de la feuille Excel que vous souhaitez mettre en gras.
xlWorkSheet.Cells[row, column].Style.Font.Bold = true;
cela fonctionne pour moi, alors essayez-le:
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();