测试如下(xls/xlsx):
//将数据写入已存在Excel
public static void writeExcel(string result, string filepath)
{
//1.创建Applicaton对象
Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.Application(); //2.得到workbook对象,打开已有的文件
Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks.Open(filepath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value); //3.指定要操作的Sheet
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[]; //在第一列的左边插入一列 1:第一列
//xlShiftToRight:向右移动单元格 xlShiftDown:向下移动单元格
//Range Columns = (Range)xSheet.Columns[, System.Type.Missing];
//Columns.Insert(XlInsertShiftDirection.xlShiftToRight, Type.Missing); //4.向相应对位置写入相应的数据
xSheet.Cells[Column(列)][Row(行)] = result; //5.保存保存WorkBook
xBook.Save();
//6.从内存中关闭Excel对象 xSheet = null;
xBook.Close();
xBook = null;
//关闭EXCEL的提示框
xApp.DisplayAlerts = false;
//Excel从内存中退出
xApp.Quit();
xApp = null;
}