mvc 导出excel

 参考:http://www.soaspx.com/dotnet/asp.net/tech/tech_20111109_8260.html

public ActionResult ExcelData()
        {
            //开始处理导出EXCEL
            List<BagInVWEntity> entityList = datas.rows.ToList();//datas.rows是已获取的数据集
            if (entityList == null || entityList.Count == 0)
            {
              //  throw new Exception("导出数据为空或者无数据!");
            }
            Excel.Application app = new Excel.Application();
            app.Workbooks.Add();
            Excel.Worksheet sheet = app.ActiveSheet;
            sheet.Cells[1, "A"] = "编号";
            sheet.Cells[1, "B"] = "日期";
            sheet.Cells[1, "C"] = "地址";
            int rowIndex = 1;
            foreach (var item in entityList)
            {
                rowIndex++;
                sheet.Cells[rowIndex, "A"] = item.No;
                sheet.Cells[rowIndex, "B"] = item.Date.ToString("yyyy-MM-dd hh:mm:ss");
                sheet.Cells[rowIndex, "C"] = item.Site;
            }
            sheet.Range["A1"].AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1);//表格的样式
            sheet.SaveAs(Path.Combine("D:\\", DateTime.Now.ToString("yyyyMMddhhmmss")  + ".xlsx"));//
            app.Quit();

            return Json(true, JsonRequestBehavior.AllowGet);
        }

存在问题:

后缀改为.xls,打开excel,可以打开,但老是报错(版本不对)

你可能感兴趣的:(导出Excel)