用设计好的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;
}
}
参考:
C#操作Excel,套用模板并对数据进行分页
Java 入门34 常见的数据结构
Java 入门 40 日志框架 项目阶段 (Java 入门 1924天 需要使用在学)
Java 入门 31 常用API 日期与时间
Java 入门 32 包装类 正则表达式 Arrays类 Lambda表达式枚举
Java 入门35 List系列集合, 几黑的并发修改异常问题 LinkedList
OpenStack yoga安装(Ubuntu)
OpenStack命令行添加网卡
Ceph删除pool
ceph osd 初始化硬盘时提示OSD::mkfs: ObjectStore::mkfs failed with error (5) Input/output error
- 最新文章
-
你有内伤吗? 英语
搞不定了,大佬带带我吧(经过几个小时的挣扎,我卸载现有环境,重新安装就好了)
Android 网络请求(Http,Socket)报错 :void com.android.okhttp.internal.http.Transport.writeRequestHeaders
Hashtable、HashMap、TreeMap
Java 泛型详解
Vector源码解析
HashSet、LinkedHashSet、TreeSet
ArrayList和LinkedList
bootstrap table表头固定
北京元宇宙外包团队:微软开发者大会2022将于下周召开,元宇宙是议题之一