• 大小: 3.83MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-20
  • 语言: C#
  • 标签: ReadAnyExcel  

资源简介

客户端不需要安装office可以正常读取任意版本的Excel

资源截图

代码片段和文件信息

using NPOI.HSSF.UserModel;
using NPOI.Openxml4Net.OPC;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Excel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        byte[] data = null;
        XSSFWorkbook wk = null;
        IWorkbook wk10 = null;
        HSSFWorkbook wk03 = null;
        IWorkbook iwk03 = null;
        OPCPackage pg = null;

        /// 
        /// 获取Excel公式计算结果
        /// 

        /// 
        /// 
        /// 
        /// 
        private static string GetCellCalculateValue(IRow row IFormulaEvaluator ev int cellIndex)
        {
            ICell cellResult = row.GetCell(cellIndex);
            string formula = cellResult.CellFormula;
            cellResult = ev.EvaluateInCell(cellResult);
            string value = cellResult.NumericCellValue.ToString();
            cellResult.SetCellFormula(formula);
            return value;
        }

        private void btnClear_Click(object sender EventArgs e)
        {
            this.dataGridViewSS.DataSource = new List();
        }

        private void btnReadAny_Click(object sender EventArgs e)
        {
             var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = “Excel files(*.xls)|*.xls;*.xlsx“

            };
            var result = openFileDialog.ShowDialog();
            IWorkbook iwkX;
            if (result == true)
            {
                using (FileStream fs = File.Open(openFileDialog.FileName FileMode.Open
               FileAccess.Read FileShare.ReadWrite))
                {
                    iwkX = WorkbookFactory.Create(fs);
                    fs.Close();
                }
                ISheet sheet = iwkX.GetSheetAt(0);
                var rows = sheet.GetRowEnumerator();
                var DataList = new List() { };
                rows.MoveNext();
                var Cols = (IRow)rows.Current;
                DataTable dt = new DataTable();
                for (int i = 0; i < Cols.LastCellNum; i++)
                {
                    dt.Columns.Add(Cols.GetCell(i).ToString());
                }
                while (rows.MoveNext())
                {
                    var row = (IRow)rows.Current;
                    var dr = dt.NewRow();
                    for (int i = 0; i < row.LastCellNum; i++)
                    {
                        var cell = row.GetCell(i);
                        if (cell == null)
                            dr[i] = ““;
                        else
                     

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

     文件     200704  2016-06-30 15:31  DLL\NPOI\ICSharpCode.SharpZipLib.dll

     文件    1606656  2016-06-30 15:32  DLL\NPOI\NPOI.dll

     文件     423936  2016-06-30 15:31  DLL\NPOI\NPOI.OOxml.dll

     文件     425038  2016-06-30 15:31  DLL\NPOI\NPOI.OOxml.xml

     文件      85504  2016-06-30 15:31  DLL\NPOI\NPOI.Openxml4Net.dll

     文件     153267  2016-06-30 15:31  DLL\NPOI\NPOI.Openxml4Net.xml

     文件    1903616  2016-06-30 15:32  DLL\NPOI\NPOI.OpenxmlFormats.dll

     文件    2137432  2016-06-30 15:32  DLL\NPOI\NPOI.xml

     文件        167  2016-07-01 09:19  Excel\App.config

     文件      11776  2016-07-01 16:04  Excel\bin\Debug\Excel.exe

     文件        167  2016-07-01 09:19  Excel\bin\Debug\Excel.exe.config

     文件      28160  2016-07-01 16:04  Excel\bin\Debug\Excel.pdb

     文件      22984  2016-07-01 16:05  Excel\bin\Debug\Excel.vshost.exe

     文件        167  2016-07-01 09:19  Excel\bin\Debug\Excel.vshost.exe.config

     文件        490  2015-10-30 15:19  Excel\bin\Debug\Excel.vshost.exe.manifest

     文件     200704  2014-07-03 10:56  Excel\bin\Debug\ICSharpCode.SharpZipLib.dll

     文件    1678848  2016-05-22 04:35  Excel\bin\Debug\NPOI.dll

     文件     494080  2016-05-22 04:35  Excel\bin\Debug\NPOI.OOxml.dll

     文件      91136  2016-05-22 04:35  Excel\bin\Debug\NPOI.Openxml4Net.dll

     文件    2121728  2016-05-22 04:35  Excel\bin\Debug\NPOI.OpenxmlFormats.dll

     文件    2254144  2016-05-22 04:35  Excel\bin\Debug\NPOI.xml

     文件       9851  2016-06-30 15:31  Excel\bin\Debug\Properties\LICENSE

     文件      22528  2016-07-01 16:01  Excel\bin\Debug\xls\Test03.xls

     文件       8082  2016-07-01 16:02  Excel\bin\Debug\xls\Test07.xlsx

     文件       8070  2016-07-01 16:02  Excel\bin\Debug\xls\Test2010.xlsx

     文件       5565  2016-07-01 09:19  Excel\Excel.csproj

     文件       3341  2016-07-01 16:04  Excel\Form1.cs

     文件       3609  2016-07-01 16:03  Excel\Form1.Designer.cs

     文件       5817  2016-07-01 16:03  Excel\Form1.resx

     文件     425038  2016-06-30 15:31  Excel\NPOI.OOxml.xml

............此处省略41个文件信息

评论

共有 条评论

相关资源