导出excel保存在指定的位置

private System.Reflection.PropertyInfo[] GetPropertyInfoArray()
{
System.Reflection.PropertyInfo[] props = null;
try
{
Type type = typeof(Temp_Check);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}

private void SaveDataToExcelFile(string fileName, List<Temp_Check> passengerList)
{
string saveFileName = Server.MapPath(@"\Upload\Import_Template\check.xls");
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

System.Reflection.PropertyInfo[] props = GetPropertyInfoArray();
//for (int i = 0; i < props.Length; i++)
//{
//表头名字
xlWorkSheet.Cells[1, 1] = "星期"; //write the column name
xlWorkSheet.Cells[1, 2] = "节次";
xlWorkSheet.Cells[1, 3] = "班级";
xlWorkSheet.Cells[1, 4] = "课程";
xlWorkSheet.Cells[1, 5] = "教师";
xlWorkSheet.Cells[1, 6] = "场地";
xlWorkSheet.Cells[1, 7] = "冲突结果";
//}
for (int i = 0; i < passengerList.Count; i++)
{
xlWorkSheet.Cells[i + 2, 1] = passengerList[i].XQ;
xlWorkSheet.Cells[i + 2, 2] = passengerList[i].JC;
xlWorkSheet.Cells[i + 2, 3] = passengerList[i].BJ;
xlWorkSheet.Cells[i + 2, 4] = passengerList[i].KC;
xlWorkSheet.Cells[i + 2, 5] = passengerList[i].JS;
xlWorkSheet.Cells[i + 2, 6] = passengerList[i].CD;
xlWorkSheet.Cells[i + 2, 7] = passengerList[i].BZ;
}
try
{
xlWorkBook.Saved = true;
xlWorkBook.SaveCopyAs(saveFileName);
xlApp.Quit();
GC.Collect();//强行销毁
}
catch (Exception ex)
{ }

}

转载于:https://www.cnblogs.com/luckys/p/7250309.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值