c#将数据导入到Excel
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); try { //excel.Visible = true; Microsoft.Office.Interop.Excel.Workbook xBook = excel.Workbooks._Open(AppDomain.CurrentDomain.BaseDirectory + "ExcelModel\\xxxxxx.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value); //excel.Cells是指你所要写入Excel表格具体格子的坐标 如: excel.Cells[3, 2] = this.comYPid.Text; excel.Cells[3, 5] = this.txtSCPH.Text; excel.Cells[3, 8] = this.dtTime.Text; excel.Cells[4, 2] = this.txtYPtype.Text; excel.Cells[4, 5] = this.txtYPljbh.Text; excel.Cells[4, 8] = this.txtSendTime.Text; excel.Cells[5, 2] = this.txtYPLJH.Text; excel.Cells[5, 5] = this.comboBox1.Text; excel.Cells[5, 8] = this.txtNomWCL.Text;//设置禁止弹出保存和覆盖的询问提示框 excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; //导出 StrSavePath =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); //保存到C:盘 xBook.SaveAs("c:\\" + StrSavePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value); xBook.Close(false, Type.Missing, Type.Missing); excel.Quit();//关闭 GC.Collect(); //打开刚才保存的文件【习惯性操作 在导出Excel时 喜欢看一下保存的数据】 当然 你也可以把上面的代码改动一下 变成保存的时候就显示出来 然后自己选择保存的地址【 //另存为】 我在这里这样处理 是因为客户要求把导出的excel保存到一个统一的大目录下 其实最后还有一部分IO的操作 没有贴出来 呵呵 Bll.Laboratory.LabPublic EXC = new Bll.Laboratory.LabPublic(); EXC.ExcelNode(this.comYPid.Text.Trim(), "XXXXXXXXXX", StrSavePath); xBook = excel.Workbooks._Open(@"C:\\" + StrSavePath + ".xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value); excel.Visible = true; } catch { } finally { ////确保Excel进程关闭 //excel.Quit(); //excel = null; }