Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add();
Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet;
try
{
workSheet.Cells[1, "A"] = "Number #";
workSheet.Cells[1, "B"] = "Firstname";
workSheet.Cells[1, "C"] = "Lastname";
workSheet.Cells[1, "D"] = "Email";
workSheet.Cells[1, "E"] = "Mobile";
// ------------------------------------------------
// Populate sheet with some real data from list
// ------------------------------------------------
int row = 2; // start row (in row 1 are header cells)
foreach (var hmn in humen)
{
workSheet.Cells[row, "A"] = hmn.Number;
workSheet.Cells[row, "B"] = hmn.FirstName;
workSheet.Cells[row, "C"] = hmn.LastName;
workSheet.Cells[row, "D"] = hmn.Email;
workSheet.Cells[row, "E"] = hmn.Mobile;
row++;
}
workSheet.Range["A1", "E" + humen.Count].AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1);
workSheet.Range["A2", "E" + humen.Count].EntireColumn.AutoFit();
string fileName = string.Format(@"{0}\Resumes_{1}_Data{2}.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), DateTime.Now.ToString("YYYY-MM-dd"), Guid.NewGuid().ToString().Substring(0, 4));
workSheet.SaveAs(fileName);
为什么不起作用?具有正确的范围,仍然没有格式化单元格
workSheet.Range["A2", "E" + humen.Count].EntireColumn.AutoFit();
不应该这样做吗?