-
大小: 3.83MB文件類(lèi)型: .rar金幣: 2下載: 0 次發(fā)布日期: 2023-10-20
- 語(yǔ)言: C#
- 標(biāo)簽: ReadAnyExcel??
資源簡(jiǎn)介
客戶端不需要安裝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公式計(jì)算結(jié)果
????????///?
????????///?
????????///?
????????///?
????????///?
????????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?????????????????{
????????????????????dt.Columns.Add(Cols.GetCell(i).ToString());
????????????????}
????????????????while?(rows.MoveNext())
????????????????{
????????????????????var?row?=?(IRow)rows.Current;
????????????????????var?dr?=?dt.NewRow();
????????????????????for?(int?i?=?0;?i?????????????????????{
????????????????????????var?cell?=?row.GetCell(i);
????????????????????????if?(cell?==?null)
????????????????????????????dr[i]?=?““;
????????????????????????else
?????????????????????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件?????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.OOxm
?????文件?????425038??2016-06-30?15:31??DLL\NPOI\NPOI.OOxm
?????文件??????85504??2016-06-30?15:31??DLL\NPOI\NPOI.Openxm
?????文件?????153267??2016-06-30?15:31??DLL\NPOI\NPOI.Openxm
?????文件????1903616??2016-06-30?15:32??DLL\NPOI\NPOI.Openxm
?????文件????2137432??2016-06-30?15:32??DLL\NPOI\NPOI.xm
?????文件????????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.OOxm
?????文件??????91136??2016-05-22?04:35??Excel\bin\Debug\NPOI.Openxm
?????文件????2121728??2016-05-22?04:35??Excel\bin\Debug\NPOI.Openxm
?????文件????2254144??2016-05-22?04:35??Excel\bin\Debug\NPOI.xm
?????文件???????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.OOxm
............此處省略41個(gè)文件信息
評(píng)論
共有 條評(píng)論