C#写入模板excel数据

简介: C#写入模板excel数据
        public ReturnNode exportReviewResult()
        {
            List<ReplaceExcelData> repData = new List<ReplaceExcelData>();
            repData.Add(new ReplaceExcelData(3, 2, "名称1"));
            repData.Add(new ReplaceExcelData(3, 7, "单位1"));
            repData.Add(new ReplaceExcelData(3, 14, "单位1"));
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            string tempFileName = localPath + "exporttemp.xlsx";
            string newFile = localPath + fileName;
            using (ExcelHelper excelHelper = new ExcelHelper(newFile))
            {
                excelHelper.ReplaceDataToExcel(tempFileName,0, repData);
                return ReturnNode.ReturnSuccess(fileName);
            }
        }
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YFAPICommon.Libs
{
    public class ReplaceExcelData
    {
        public int rowIndex { set; get; }
        public int cellIndex { set; get; }
        public object value { set; get; }
        public ReplaceExcelData(int _row,int _cell,object _value)
        {
            this.rowIndex = _row;
            this.cellIndex = _cell;
            this.value = _value;
        }
    }
    class ExcelHelper : IDisposable
    {
        private string fileName = null; //文件名
        private NPOI.SS.UserModel.IWorkbook workbook = null;
        private FileStream fs = null;
        private bool disposed;
        public ExcelHelper(string fileName)
        {
            this.fileName = fileName;
            disposed = false;
        }
        public int ReplaceDataToExcel(string tempFilePath,int sheetIndex,List<ReplaceExcelData> replaceData)
        {
            int count = 0;
            ISheet sheet = null;
            fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            using (FileStream tempfs = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
            {
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                    workbook = new XSSFWorkbook(tempfs);
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                    workbook = new HSSFWorkbook(tempfs);
            }
            try
            {
                if (workbook != null)
                {
                    sheet = workbook.GetSheetAt(sheetIndex);
                }
                else
                {
                    return -1;
                }
                foreach(var node in replaceData)
                {
                    var row = sheet.GetRow(node.rowIndex);
                    var valuestr = node.value.ToString();
                    double valDouble = 0;
                    if(double.TryParse(valuestr,out valDouble))
                    {
                        row.GetCell(node.cellIndex).SetCellValue(valDouble);
                    }
                    else
                    {
                        row.GetCell(node.cellIndex).SetCellValue(valuestr);
                    }
                    count++;
                }
                workbook.Write(fs); //写入到excel
                return count;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                return -1;
            }
        }
    }
}
相关文章
|
2月前
|
关系型数据库 MySQL Shell
不通过navicat工具怎么把查询数据导出到excel表中
不通过navicat工具怎么把查询数据导出到excel表中
32 0
|
6天前
|
SQL C# 数据库
EPPlus库的安装和使用 C# 中 Excel的导入和导出
本文介绍了如何使用EPPlus库在C#中实现Excel的导入和导出功能。首先,通过NuGet包管理器安装EPPlus库,然后提供了将DataGridView数据导出到Excel的步骤和代码示例,包括将DataGridView转换为DataTable和使用EPPlus将DataTable导出为Excel文件。接着,介绍了如何将Excel数据导入到数据库中,包括读取Excel文件、解析数据、执行SQL插入操作。
EPPlus库的安装和使用 C# 中 Excel的导入和导出
|
21天前
|
存储 C# 开发者
枚举与结构体的应用:C#中的数据组织艺术
在C#编程中,枚举(`enum`)和结构体(`struct`)是非常重要的数据类型。枚举用于定义命名常量集合,提高代码可读性;结构体则封装相关数据字段,适合小型数据集。本文从基本概念入手,探讨它们的使用技巧、常见问题及解决方案,帮助开发者更好地利用这些特性构建健壮的应用程序。
23 8
|
24天前
|
数据采集 存储 数据挖掘
使用Python读取Excel数据
本文介绍了如何使用Python的`pandas`库读取和操作Excel文件。首先,需要安装`pandas`和`openpyxl`库。接着,通过`read_excel`函数读取Excel数据,并展示了读取特定工作表、查看数据以及计算平均值等操作。此外,还介绍了选择特定列、筛选数据和数据清洗等常用操作。`pandas`是一个强大且易用的工具,适用于日常数据处理工作。
|
2月前
|
SQL JSON 关系型数据库
n种方式教你用python读写excel等数据文件
n种方式教你用python读写excel等数据文件
|
2月前
|
存储 Java Apache
|
2月前
|
数据可视化 Python
我是如何把python获取到的数据写入Excel的?
我是如何把python获取到的数据写入Excel的?
39 2
|
2月前
|
索引 Python
Python基于Excel多列长度不定的数据怎么绘制折线图?
本文档详述了如何运用Python从CSV格式的Excel文件中读取特定范围的数据,并基于这些数据绘制多条折线图。文件的第一列代表循环增长的时间序列,后续各列包含不同属性的数据。通过指定起始与结束行数,可选取一个完整的时间循环周期内的数据进行绘图。每列数据以不同颜色和线型表示,并且图片长度会根据时间序列的长度动态调整,确保图表清晰易读。最终生成的图表将保存至指定文件夹。
|
2月前
|
关系型数据库 MySQL Windows
MySQL数据导入:MySQL 导入 Excel 文件.md
MySQL数据导入:MySQL 导入 Excel 文件.md
|
2月前
|
数据管理 数据处理 数据库
分享一个导出数据到 Excel 的解决方案
分享一个导出数据到 Excel 的解决方案