首页 > 其他分享 >.NET实现 导出带超链接的Excel数据

.NET实现 导出带超链接的Excel数据

时间:2022-11-17 22:35:36浏览次数:53  
标签:string Excel 超链接 file using var new NET path

using File4._5.model;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace File4._5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            var result = "";
            try
            {
                bool isAll = ckAll.Checked;
                var fileModels = new List<FileModel>();

                var folderPath = txtPath.Text.Trim();

                var fileInfos = new List<FileInfo>();
                if (isAll)
                {

                    var folders = Directory.GetDirectories(folderPath);
                    foreach (var folderStr in folders)
                    {
                        DirectoryInfo folder = new DirectoryInfo(folderStr);
                        foreach (var file in folder.GetFiles())
                        {
                            Console.WriteLine(file.FullName);
                            result += file.FullName + Environment.NewLine;
                            fileModels.Add(new FileModel()
                            {
                                file_name = file.Name,
                                file_path = file.FullName
                            }); ;

                        }
                    }
                }
                else
                {

                    DirectoryInfo folder = new DirectoryInfo(folderPath);
                    foreach (var file in folder.GetFiles())
                    {
                        Console.WriteLine(file.FullName);
                        result += file.FullName + Environment.NewLine;
                        fileModels.Add(new FileModel()
                        {
                            file_name = file.Name,
                            file_path = file.FullName
                        }); ;

                    }
                }

                Dictionary<string, string> thDic = new Dictionary<string, string>();
                thDic.Add("file_name", "文件名");
                thDic.Add("file_path", "文件路径");

                var excelPath = folderPath + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

                //   NPOIHelper.PageSaveToXls(excelPath, fileModels, thDic);

                var respSre = saveExecl(excelPath, fileModels);

                

                if (string.IsNullOrEmpty(respSre))
                {
                    txtShow.Text = "";
                    txtShow.Text = excelPath;
                    MessageBox.Show("保存成功");
                }
                else
                {
                    MessageBox.Show("保存失败:" + respSre);
                }

            }
            catch (Exception ex)
            {
                result = "异常:" + ex.Message + Environment.NewLine + "代码:" + ex.StackTrace;
            }
            Console.Read();
        }

        private string saveExecl(string path, List<FileModel> list)
        {

            //excel
            IWorkbook book = null;
            //excel的sheet页
            ISheet sheet = null;

            try
            {
                using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    //扩展名
                    string extension = Path.GetExtension(path);
                    switch (extension.ToLower())
                    {
                        case ".xls":
                            book = new HSSFWorkbook();//NPOI.dll
                            break;
                        case ".xlsx":
                            book = new XSSFWorkbook();//NPOI.OOXML.dll
                            break;
                    }

                    sheet = book.CreateSheet();
                    //设置表头
                    var header = sheet.CreateRow(0);
                    header.CreateCell(0).SetCellValue("文件名称");
                    header.CreateCell(1).SetCellValue("文件路径");

                    //创建表的内容
                    var trIndex = 1;
                    list.ForEach(i =>
                    {
                        HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.Url);
                        link.Address = i.file_path;
                        var tr = sheet.CreateRow(trIndex);
                        tr.CreateCell(0).SetCellValue(i.file_name);
                        var cellName = tr.CreateCell(0);
                        cellName.SetCellValue(i.file_name);
                        cellName.Hyperlink = link;

                        var cell = tr.CreateCell(1);
                        cell.SetCellValue(i.file_path);
                       
                       // cell.Hyperlink = link;
                        trIndex++;
                    });




                    book.Write(stream);
                    stream.Close();


                }
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
    }
}

model 类

public class FileModel
    {
        public string file_name { get; set; }

        public string file_path { get; set; }
    }

 

标签:string,Excel,超链接,file,using,var,new,NET,path
From: https://www.cnblogs.com/WQ1992/p/15354894.html

相关文章

  • 单服务器,以docker发布NET服务只需一键!
     准备1.dockerhub 私有云  这里我用的是阿里的容器镜像服务个人2.服务器(略)3.portainerdockerrun-d-p9000:9000-v/var/run/docker.sock:/var/run/doc......
  • Pod的理解(chrono《kubernetes入门实战课》笔记整理)
     【为什么要有pod】pod,直译为豌豆荚,形象地描述了它的功能,它内部包含很多豆子,这一颗颗豆子,就是每个组件、成员。既然pod也是为了运行容器,为什么不直接用docker运行呢?因为......
  • Kubernetes日志采集Sidecar模式介绍
    摘要:DaemonSet和Sidecar模式各有优缺点,目前没有哪种方式可以适用于所有场景。因此我们阿里云日志服务同时支持了DaemonSet以及Sidecar两种方式,并对每种方式进行了一些额外......
  • drf结合rest-pandas实现下载excel文件
    目录drf结合rest-pandas实现下载excel文件下载rest-pandas使用方法注意事项drf结合rest-pandas实现下载excel文件下载rest-pandaspipinstallrest-pandas使用方法在......
  • Net6 操作时序数据库 Influxdb ,插入数据
    十年河东,十年河西,莫欺少年穷学无止境,精益求精1、引入组件  2、书写代码publicclassInfluxDto{privatereadonlyILogger<InfluxDto>logger;......
  • .NET Core 项目Linux环境下生成二维码
    问题:公司系统开发中,需要对企微授权链接进行二维码生成,然后向客户提供;当然,首当其冲想到的是使用ZXing.NET库进行实现,毕竟生成简单二维码也就那几句代码;然而,在本地环境中,一......
  • .NET Core 获取程序运行环境信息与反射的应用
    笔者的九篇反射系统文章已经完结,但是笔者会持续更新反射在日常撸码中的应用。本篇内容主要是.NETCore获取运行环境信息、利用反射更加方便地处理数据。本篇内容有:Runti......
  • Kubeadm搭建kubernetes集群
    Kubeadm搭建kubernetes集群环境说明|角色|ip|操作系统|组件||–|–|–||master|192.168.203.100|centos8|docker,kubectl,kubeadm,kubelet||node2......
  • .net core 获取本地ip及request请求端口
    1.获取ip和端口stringstr=(Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString()+":"+Request.HttpContext.Connection.LocalPort); 输出s......
  • 基于centos7.9二进制部署kubernetes1.25.4
    基于centos7.9二进制部署kubernetes1.25.41、环境准备IP地址主机角色系统192.168.100.153k8s-master01K8S集群主节点Centos7.9192.168.100.154k8s-ma......