简体   繁体   English

如何在excel c# win表单中编写粗体文本

[英]how to write bold text in excel c# win forms

I use this code and want to make bold latter on heading please help me.我使用此代码并希望在标题上加粗,请帮助我。

want to create designed excel sheet that looking good after export.想要创建导出后看起来不错的设计好的 excel 表。 in other ways that modified but want to use this way to modified excel sheet.以其他方式修改但想用这种方式修改excel表。

StreamWriter wr = new StreamWriter(@"D:\\test.xls");


foreach (string sHeader in Header)
{
      wr.Write(sHeader);
      wr.WriteLine();
}
wr.Write("");
wr.WriteLine();

for (int i = 0; i < Dt.Columns.Count; i++)
{
      wr.Write(Dt.Columns[i].ToString().ToUpper() + "\t");
}

wr.WriteLine();

//write rows to excel file
for (int i = 0; i < (Dt.Rows.Count); i++)
{
     for (int j = 0; j < Dt.Columns.Count; j++)
     {
            if (Dt.Rows[i][j] != null)
            {
                wr.Write(Convert.ToString(Dt.Rows[i][j]) + "\t");
            }
            else
            {
                wr.Write("\t");
            }
      }
      //go to next line
      wr.WriteLine();
}
//close file
wr.Close();

add this reference and try this "using Microsoft.Office.Interop.Excel.ApplicationClass"添加此参考并尝试使用“使用 Microsoft.Office.Interop.Excel.ApplicationClass”

 try
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.FileName = "Total Expiry Inventories Data ";
        sfd.DefaultExt = "xls";
        sfd.Filter = "xlsx files(*.xlsx)|*.xlsx";
        if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
        {
            return;
        }

        Excel.Application ExcelApp = new Excel.Application();
        Excel.Workbook workbook = ExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
        Excel.Worksheet worksheet1 = (Excel.Worksheet)workbook.Worksheets[1];
        worksheet1.Name = "Expiry Data";
        for (int i = 1; i < GrdViewData.Columns.Count + 1; i++)
        {
            worksheet1.Cells[1, i] = GrdViewData.Columns[i - 1].HeaderText;
            worksheet1.Cells[1, i].Font.Bold = true;


        }
        for (int i = 0; i < GrdViewData.Rows.Count; i++)
        {
            for (int j = 0; j < GrdViewData.Columns.Count; j++)
            {
                worksheet1.Cells[i + 2, j + 1] = GrdViewData.Rows[i].Cells[j].Value.ToString();
            }
        }
        worksheet1.Rows.Font.Size = 12;
        //  Excel.Range range_Consolidated = worksheet1.Rows.get_Range("a1", "d1");
        // range_Consolidated.Font.Bold = true;

        // range_Consolidated.Font.Italic = true;

        string ExcelFileName = sfd.FileName;
        workbook.SaveAs(ExcelFileName);
        workbook.Close(false, ExcelFileName, Missing.Value);
        ExcelApp.Quit();

        ExcelApp = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        MessageBox.Show("File Saved! you can open it from\n  '" + sfd.FileName + "'", "EXPORT", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM