• 大小: 981KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: C#
  • 标签: excelC#  

资源简介

c#类库 使用NPOI操作excel,无需安装excel 可插入指定位置, 在指定位置插入datatable

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using NPOI.HSSF.UserModel;

namespace SXLibrary
{
    public class ExcelObj
    {
        /// 
        /// 插入到excel的cell
        /// 

        /// excel文件名
        /// 目标 (行,列,值)
        /// 默认sheetName可以不写
        public static void WriteToExcelCell(string fileName Listject>> customCell string sheetName = “sheet1“)
        {
            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(sheetName);
            foreach (var item in customCell)
            {
                int r = item.Item1;
                int c = item.Item2;
                object content = item.Item3;
                NPOI.SS.UserModel.IRow row = null;
                if (sheet.GetRow(r) == null)
                {
                    row = sheet.CreateRow(r);
                }
                else
                {
                    row = sheet.GetRow(r);
                }
                NPOI.SS.UserModel.ICell cell = row.CreateCell(c);
                cell.SetCellValue(content.ToString());
                string objType = content.GetType().ToString();
                #region 类型转换
                switch (objType)
                {
                    case “System.String“://字符串类型
                        cell.SetCellValue(content.ToString());
                        break;
                    case “System.DateTime“://日期类型  
                        DateTime dateV;
                        DateTime.TryParse(content.ToString() out dateV);
                        string strtime = dateV.ToString(“yyyy-MM-dd HH:mm:ss“);
                        if (strtime.Substring(11 8) == “00:00:00“)
                        {
                            strtime = dateV.ToString(“yyyy-MM-dd“);
                        }
                        cell.SetCellValue(strtime);
                        break;
                    case “System.Boolean“://布尔型  
                        bool boolV = false;
                        bool.TryParse(content.ToString() out boolV);
                        cell.SetCellValue(boolV);
                        break;

                    case “System.Int16“://整型  
                    case “System.Int32“:
                    case “System.Int64“:
                    case “System.Byte“:
                        int intV = 0;
                        int.TryParse(content.ToString() out intV);
                        cell.SetCellValue(intV);
                        break;
                    case “System.Decimal“://浮点型  
                    case “System.Double“:
                        double doubV = 0;
                        double.TryParse(content.ToString() out doubV)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       8704  2014-01-14 11:09  ExcelObj\ExcelObj\bin\Debug\ExcelObj.dll

     文件      17920  2014-01-14 11:09  ExcelObj\ExcelObj\bin\Debug\ExcelObj.pdb

     文件    1600000  2014-01-14 11:07  ExcelObj\ExcelObj\bin\Debug\NPOI.dll

     文件       9114  2014-01-14 11:09  ExcelObj\ExcelObj\ExcelObj.cs

     文件       2688  2014-01-14 11:09  ExcelObj\ExcelObj\ExcelObj.csproj

     文件    1600000  2014-01-14 11:07  ExcelObj\ExcelObj\NPOI.dll

     文件       5659  2014-01-14 11:09  ExcelObj\ExcelObj\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        308  2014-01-14 11:09  ExcelObj\ExcelObj\obj\Debug\ExcelObj.csproj.FileListAbsolute.txt

     文件       8704  2014-01-14 11:09  ExcelObj\ExcelObj\obj\Debug\ExcelObj.dll

     文件      17920  2014-01-14 11:09  ExcelObj\ExcelObj\obj\Debug\ExcelObj.pdb

     文件       7199  2014-01-14 11:09  ExcelObj\ExcelObj\obj\Debug\ResolveAssemblyReference.cache

     文件       1366  2014-01-14 11:06  ExcelObj\ExcelObj\Properties\AssemblyInfo.cs

     文件        914  2014-01-14 11:06  ExcelObj\ExcelObj.sln

    ..A..H.     14848  2014-01-14 11:10  ExcelObj\ExcelObj.suo

     目录          0  2014-01-14 11:06  ExcelObj\ExcelObj\obj\Debug\TempPE

     目录          0  2014-01-21 15:02  ExcelObj\ExcelObj\bin\Debug

     目录          0  2014-01-14 11:08  ExcelObj\ExcelObj\bin\Release

     目录          0  2014-01-21 15:02  ExcelObj\ExcelObj\obj\Debug

     目录          0  2014-01-21 15:02  ExcelObj\ExcelObj\bin

     目录          0  2014-01-14 11:06  ExcelObj\ExcelObj\obj

     目录          0  2014-01-21 15:02  ExcelObj\ExcelObj\Properties

     目录          0  2014-01-21 15:02  ExcelObj\ExcelObj

     目录          0  2014-01-21 15:02  ExcelObj

----------- ---------  ---------- -----  ----

              3295344                    23


评论

共有 条评论

相关资源