用设计好的Excel模板,复制一下,往里面添加数据比较省事。
模板
导出文件:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind();
}
private void Bind()
{
//模板文件
string TempletFileName = Server.MapPath("template/") + "template.xlsx";
//导出文件
string ReportFileName = Server.MapPath("xls/") + "out.xlsx";
string strTempletFile = Path.GetFileName(TempletFileName);
//将模板文件复制到输出文件
FileInfo mode = new FileInfo(TempletFileName);
mode.CopyTo(ReportFileName, true);
//打开excel
object missing = Missing.Value;
Application app = null;
Workbook wb = null;
Worksheet ws = null;
Range r = null;
//
app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Open(ReportFileName, false, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
app.Visible = true;
//得到WorkSheet对象
ws = (Worksheet)wb.Worksheets.get_Item(1);
//添加或修改WorkSheet里的数据
ws.Cells[1, 1] = "100";
ws.Cells[2, 1] = "100";
ws.Cells[2, 2] = "100";
//代码里写个公式
r = (Range)ws.Cells[2, 3];
r.Formula = "=A2*B2";
//输出Excel文件并退出
wb.Save();
wb.Close(null, null, null);
app.Workbooks.Close();
app.Application.Quit();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
ws = null;
wb = null;
app = null;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Bind();
}
private void Bind()
{
//模板文件
string TempletFileName = Server.MapPath("template/") + "template.xlsx";
//导出文件
string ReportFileName = Server.MapPath("xls/") + "out.xlsx";
string strTempletFile = Path.GetFileName(TempletFileName);
//将模板文件复制到输出文件
FileInfo mode = new FileInfo(TempletFileName);
mode.CopyTo(ReportFileName, true);
//打开excel
object missing = Missing.Value;
Application app = null;
Workbook wb = null;
Worksheet ws = null;
Range r = null;
//
app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Open(ReportFileName, false, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
app.Visible = true;
//得到WorkSheet对象
ws = (Worksheet)wb.Worksheets.get_Item(1);
//添加或修改WorkSheet里的数据
ws.Cells[1, 1] = "100";
ws.Cells[2, 1] = "100";
ws.Cells[2, 2] = "100";
//代码里写个公式
r = (Range)ws.Cells[2, 3];
r.Formula = "=A2*B2";
//输出Excel文件并退出
wb.Save();
wb.Close(null, null, null);
app.Workbooks.Close();
app.Application.Quit();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
ws = null;
wb = null;
app = null;
}
}
参考:
利用scp传输文件小结
SSH无密码登录配置小结
vim 清空
mysql root用户 远程登录其它机器,看不到数据库
音视频编解码开发经验2
nginx、php-fpm、mysql用户权限解析
ffmpeg处理RTMP流媒体的命令 发送流媒体的命令(UDP,RTP,RTMP)
音视频编解码开发经验1
mysql LAST_INSERT_ID 使用与注意事项