gpt4 book ai didi

c#,Microsoft Interop Excel,更改所选范围的字体样式

转载 作者:行者123 更新时间:2023-11-30 20:28:18 28 4
gpt4 key购买 nike

我需要更改Excel工作表中某些单元格的字体样式(字体类型)。Excel工作表由Microsoft.Office.Interop.Excel生成

我想将 B3 到 B100 范围更改为 Arial 字体类型。这是我的代码

Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

app.Visible = true;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from gridview";
worksheet.get_Range("B3", "B100").Cells.Font.FontStyle = "Arial";

for (int i = 1; i < records_datagridview.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = records_datagridview.Columns[i - 1].HeaderText;
}

for (int i = 0; i < records_datagridview.Rows.Count; i++)
{
for (int j = 0; j < records_datagridview.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = records_datagridview.Rows[i].Cells[j].Value.ToString();
}
}

String file_path = fbd.SelectedPath + "/records_sheet_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_.xlsx";

workbook.SaveAs(file_path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

app.Quit();

我用过

worksheet.get_Range("B3", "B4").Cells.Font.FontStyle = "Arial";

但是没有效果谁能帮我解决这个问题

最佳答案

把你的代码改成这样:

worksheet.get_Range("B3", "B4").Cells.Font.Name = "Arial";

提示:在许多情况下,您可以自己回答此类问题:只需录制一个 Excel 宏并查看生成的代码。即使它是 VBA,而不是 C#,您也会经常看到您需要的东西。

关于c#,Microsoft Interop Excel,更改所选范围的字体样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47731117/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com