在Excel中突出显示单元格/使其变成粗体

在一个小组项目中,我必须写一个表格到一个Excel工作表。 我已经完成了,但是我需要突出显示数据所在的表格区域,可以在这些图片中看到:

https://gyazo.com/51c57897d9a1ce8df000d6ff0f18de20

https://gyazo.com/bcc879cd7d1c5f12ccb853490dca22f2

第一张图片显示了它应该看起来没有数据,第二张图片显示了我的当前文件加载的数据。

是否有可能突出显示在数据应该是第一张图片?

我一直没有能够find任何来源处理这个在线。

这是创buildExcel工作表的代码。 请注意,我没有正确的格式化代码,但它应该是可读的:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ExcelLibrary.SpreadSheet; using System.Data; using System.IO; using System.Windows; using Excel = Microsoft.Office.Interop.Excel; namespace ProjectXstaal { class Print { public void LoadToExcel(DataTable dt) { string filetest = "filetest.xlsx"; double allHours = 0; double overtimeHours = 0; if (File.Exists(filetest)) { File.Delete(filetest); } Excel.Application oApp; Excel.Worksheet oSheet; Excel.Workbook oBook; oApp = new Excel.Application(); oBook = oApp.Workbooks.Add(); Excel.Worksheet ws = (Excel.Worksheet)oApp.ActiveSheet; // Calculates the total hours worked and total overtime hours. not important for the question for (int i = 0; i < dt.Rows.Count; i++) { string overtimeTemp = dt.Rows[i][3].ToString(); string temp = dt.Rows[i][2].ToString(); double temp2 = Double.Parse(temp); double temp3 = Double.Parse(overtimeTemp); overtimeHours += temp3; allHours += temp2 + temp3; if (i >= 40) { break; } } // some formatting of the page ws.Cells[3, 1] = "Tidsperiode:"; ws.Cells[3, 5] = "NAVN"; ws.Cells[3, 6] = dt.Rows[1][7].ToString(); ws.Cells[1, 5] = DateTime.Now.Year.ToString(); ws.Cells[1, 3] = "ARBEJDSSEDDEL"; ws.Cells[40, 1] = "SAMMENLAGT FOR UGEN"; ws.Cells[40, 5] = allHours; ws.Cells[41, 1] = "UGENS TIMER FORDELES PÅ"; ws.Cells[43, 5] = overtimeHours; ws.Cells[42, 5] = allHours - overtimeHours; ws.Cells[42, 4] = "NORMALTIMER"; ws.Cells[43, 4] = "OVERARBEJDE"; ws.Cells[6, 1] = dt.Columns[1].ToString(); ws.Cells[6, 2] = dt.Columns[5].ToString(); ws.Cells[6, 3] = dt.Columns[0].ToString(); ws.Cells[6, 4] = dt.Columns[4].ToString(); ws.Cells[6, 5] = dt.Columns[2].ToString(); ws.Cells[6, 6] = dt.Columns[3].ToString(); ws.Cells[6, 7] = dt.Columns[6].ToString(); //prints the datatable to the excel sheet and stops when it cant fit anymore information for (int i = 0; i < dt.Rows.Count; i++) { ws.Cells[i + 7, 1] = dt.Rows[i][1].ToString(); ws.Cells[i + 7, 2] = dt.Rows[i][5].ToString(); ws.Cells[i + 7, 3] = dt.Rows[i][0].ToString(); ws.Cells[i + 7, 4] = dt.Rows[i][4].ToString(); ws.Cells[i + 7, 5] = dt.Rows[i][2].ToString(); ws.Cells[i + 7, 6] = dt.Rows[i][3].ToString(); ws.Cells[i + 7, 7] = dt.Rows[i][6].ToString(); ws.Cells[i + 7, 1].Bold = true; if (i >= 32) { break; } } // Sets column width of the data. ws.Range["A6"].ColumnWidth = 11; ws.Range["B6"].ColumnWidth = 10; ws.Range["C6"].ColumnWidth = 6.7; ws.Range["D6"].ColumnWidth = 11; ws.Range["E6"].ColumnWidth = 11; ws.Range["F6"].ColumnWidth = 15; ws.Range["G6"].ColumnWidth = 15; oBook.SaveAs(filetest); oBook.Close(); oApp.Quit(); MessageBox.Show("Det virker måske"); } } } 

如果你现在应该大胆的范围,使用:

 Microsoft.Office.Interop.Excel.Range range = xlWorkSheet.get_Range("A1:A6","G1:G6"); range.Font.Bold = true; 

在这个例子中,假设在左上angular的单元格是“A1”,前6行和前7 7列变粗。

我希望这有帮助!