c#,Microsoft Interop Excel,更改选定范围的字体样式
c#, Microsoft Interop Excel , change font style for s selected range
我需要更改 Excel sheet 中某些单元格的字体样式 (font type )。
Excel sheet 由 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#,但你会经常看到你需要的。
您需要先定义样式。
Excel.Style style = workbook.Styles.Add("NewStyle");
style.Font.Name = "Arial";
然后您需要定义单元格范围并应用新样式。
Excel.Range rangeStyles = app.get_Range("B3", "B100");
rangeStyles.Style = "NewStyle";
有关详细信息,请查看 Microsoft 文档页面 https://msdn.microsoft.com/en-us/library/f1hh9fza.aspx
我需要更改 Excel sheet 中某些单元格的字体样式 (font type )。 Excel sheet 由 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#,但你会经常看到你需要的。
您需要先定义样式。
Excel.Style style = workbook.Styles.Add("NewStyle");
style.Font.Name = "Arial";
然后您需要定义单元格范围并应用新样式。
Excel.Range rangeStyles = app.get_Range("B3", "B100");
rangeStyles.Style = "NewStyle";
有关详细信息,请查看 Microsoft 文档页面 https://msdn.microsoft.com/en-us/library/f1hh9fza.aspx