首页 > 编程语言 >C#-生成Excel文件

C#-生成Excel文件

时间:2023-01-12 18:11:35浏览次数:44  
标签:const Missing C# Cells Excel 生成 int ws public

引入命名空间:

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

创建Sheet:  

var app = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = app.Workbooks.Add(System.Reflection.Missing.Value);
Worksheet ws = wb.Sheets[1];

设置格式:

Range allRange = ws.Range[ws.Cells[1, 1], ws.Cells[5, 5]];
Range titleRange = ws.Range[ws.Cells[1, 1], ws.Cells[1, 5]];
Range mainRange = ws.Range[ws.Cells[1, 2], ws.Cells[5, 2]];
allRange.Font.Name = "Times New Roman";
allRange.WrapText = false;
titleRange.Font.Bold = true;
titleRange.Font.Size = 15;
titleRange.EntireColumn.AutoFit();
titleRange.Borders.LineStyle = XlLineStyle.xlContinuous;
titleRange.Interior.ColorIndex = ExcelColor.green;
mainRange.Interior.ColorIndex = ExcelColor.cyan;

填充内容:

ws.Cells[1, 1] = "ID";
ws.Cells[1, 2] = "Name";
ws.Cells[1, 3] = "Age";
ws.Cells[1, 4] = "Number";
ws.Cells[1, 5] = "Address";
for (int j = 1; j < 5; j++)
{
    ws.Cells[1 + j, 1] = j;
    ws.Cells[1 + j, 2] = "名称" + j;
    ws.Cells[1 + j, 3] = 20 + j;
    ws.Cells[1 + j, 4] = 123450 + j;
    ws.Cells[1 + j, 5] = "地址" + j;
}

保存到文件,释放进程:

ws.SaveAs(file, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
wb.Close(false, Type.Missing, Type.Missing);
wb = null;
app.Quit();
GC.Collect();
KeyProcess.Kill(app);

ColorIndex:

class ExcelColor
{
    public const int transparent = 0;
    public const int black = 1;
    public const int white = 2;
    public const int red = 3;
    public const int green = 4;
    public const int blue = 33;
    public const int yellow = 6;
    public const int pink = 7;
    public const int cyan = 8;
    public const int darkRed = 9;
    public const int dark = 29;
    public const int gray = 15;
    public const int darkCyan = 31;
    public const int darkSlateBlue = 51;
}

释放进程函数:

class KeyProcess
{
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
    public static void Kill(Microsoft.Office.Interop.Excel.Application excel)
    {
        IntPtr add = new IntPtr(excel.Hwnd);
        int pid = 0;
        GetWindowThreadProcessId(add, out pid);
        System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(pid);
        p.Kill();
    }
}

 

标签:const,Missing,C#,Cells,Excel,生成,int,ws,public
From: https://www.cnblogs.com/cfsl/p/17047463.html

相关文章

  • Gdevops广州站:支撑700亿数据量的ClickHouse高可用架构实践
    2021Gdevops全球敏捷运维峰会- 广州站,将在5月28日盛大举办。Gdevops经过创办6年成功举行近20场大会的经验积淀,本次峰会将结合行业趋势与技术热点,携手腾讯、京东、网易、......
  • Nik Collection 5 for Mac(PS滤镜插件套装) v5.4.0/v4.3.6中文激活版
    NikCollection中文版是一款ps滤镜插件套装,其包含了八款ps插件,分别是NikColorEfex、NikSilverEfex、NikAnalogEfex、NikViveza、NikDfine、NikPerspectiveEfex,Ni......
  • 初识C语言
    1、对编程而言,可移植性意味着什么?在一种系统中编写的C程序稍作修改或不修改就可以在其他系统运行。2、编程的七个主要步骤是什么?定义程序的目标设计程序编写程序编......
  • 使用Stream进行List转Map踩坑Duplicate key错误
    报错代码myList.stream().collect(Collectors.toMap(MyDto::getCd,MyDto::getNm));报错信息java.lang.IllegalStateException:Duplicatekey000001-01-000000000000......
  • 动态修改网页的 icon 和 title
    在public的index.html中使用link和title占位(内容随意,获取到数据之后此处的内容会被覆盖)在App.vue中调用接口获取数据,找到link和title元素,将获取的数据赋值 ......
  • S2-061 CVE-2020-17530 远程代码执行
    漏洞名称S2-061CVE-2020-17530远程代码执行利用条件Struts2.0.0-Struts2.5.25漏洞原理s2-061漏洞产生的原因是Struts2会对某些标签属性(比如id,其他属性有待寻......
  • Linux内核为什么会发生soft lockup?【转】
    转自:https://blog.csdn.net/21cnbao/article/details/108250786提到softlockup,大家都不会陌生:BUG: soft lockup - CPU#3 stuck for 23s! [kworker/3:0:32]......
  • free5gc+UERANSIM的5GAKA源码和抓包分析
    5G_AKA的整体流程 由上图可以看到5G_AKA的验证流程就是首先UE验证核心网,通过后核心网在对UE进行验证的一个认证注册流程。核心网UDM产生鉴权向量AV并传输(Authenticat......
  • glibc升级
    1、检查当前glibc版本[[email protected]]#ldd--versionldd(GNUlibc)2.17Copyright2012FreeSoftwareFoundation,Inc.Thisisfreesoftware;seethesourc......
  • Oracle数据库软件包MD5校验HASH值汇总
    ################################Linux_x64_11gR211.2.0.4################################p13390677_112040_Linux-x86-64_1of7.zip大小:1395582860字节 MD5:......