Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions main/HSSF/UserModel/HSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace NPOI.HSSF.UserModel
using NPOI.HSSF.UserModel.helpers;

using SixLabors.Fonts;
using System.Data;

/// <summary>
/// High level representation of a worksheet.
Expand Down Expand Up @@ -3391,6 +3392,11 @@ IEnumerator<IRow> IEnumerable<IRow>.GetEnumerator()
return rows.Values.GetEnumerator();
}

public DataTable ToDataTable(bool firstRowAsHeader = false, bool showCalculatedValue = false)
{
return SheetUtil.ToDataTable(this, firstRowAsHeader, showCalculatedValue);
}

public NCellRange Cells
{
get
Expand Down
21 changes: 13 additions & 8 deletions main/HSSF/UserModel/HSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ limitations Under the License.

namespace NPOI.HSSF.UserModel
{

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using NPOI.DDF;
using NPOI.HPSF;
using NPOI.HSSF.Model;
Expand All @@ -38,6 +30,14 @@ namespace NPOI.HSSF.UserModel
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;


/// <summary>
Expand Down Expand Up @@ -2499,6 +2499,11 @@ public bool IsDate1904()
return Workbook.IsUsing1904DateWindowing;
}

public DataSet ToDataSet(bool firstRowAsHeader = false, bool showCalculatedValue = false)
{
return WorkbookUtil.ToDataSet(this, firstRowAsHeader, showCalculatedValue);
}

public void Dispose()
{
this.Close();
Expand Down
9 changes: 9 additions & 0 deletions main/SS/UserModel/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace NPOI.SS.UserModel
using NPOI.SS.Util;
using System.Collections;
using NPOI.Util;
using System.Data;

/// <summary>
/// Indicate the position of the margin. One of left, right, top and bottom.
Expand Down Expand Up @@ -933,5 +934,13 @@ bool Autobreaks

NCellRange Cells { get; }
NRowRange Rows { get; }

/// <summary>
/// Convert a sheet to a DataTable. Note that merged cells are not supported
/// </summary>
/// <param name="firstRowAsHeader">whether first row is column header</param>
/// <param name="showCalculatedValue">whether to show evaluated value of a formula in the cell</param>
/// <returns></returns>
DataTable ToDataTable(bool firstRowAsHeader = false, bool showCalculatedValue = false);
}
}
9 changes: 6 additions & 3 deletions main/SS/UserModel/Workbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ limitations under the License.

namespace NPOI.SS.UserModel
{
using NPOI.SS.Formula.UDF;
using NPOI.Util;
using System;
using System.Collections;
using System.IO;
using NPOI.SS.Formula.UDF;
using System.Collections.Generic;
using NPOI.Util;
using System.Data;
using System.IO;

/// <summary>
/// High level interface of a Excel workbook. This is the first object most users
Expand Down Expand Up @@ -445,5 +446,7 @@ public interface IWorkbook : ICloseable, IDisposable
/// <return>the index of the added ole object, i.e. the storage id</return>
/// <exception cref="IOException">if the object can't be embedded</exception>
int AddOlePackage(byte[] oleData, String label, String fileName, String command);

DataSet ToDataSet(bool firstRowAsHeader = false, bool showCalculatedValue = false);
}
}
111 changes: 107 additions & 4 deletions main/SS/Util/SheetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.

namespace NPOI.SS.Util
{
using System;

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using SixLabors.Fonts;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using SixLabors.Fonts;
using System.Linq;
using System.Data;
using System.Globalization;
using System.Linq;

/**
* Helper methods for when working with Usermodel sheets
Expand Down Expand Up @@ -913,6 +914,108 @@ public static ICell GetCellWithMerges(ISheet sheet, int rowIx, int colIx)
// live within any merged regions
return null;
}
/// <summary>
/// Convert a sheet to a DataTable. Note that merged cells are not supported
/// </summary>
/// <param name="sheet">original sheet</param>
/// <param name="firstRowAsHeader">whether first row is column header</param>
/// <param name="showCalculatedFormulaValue">whether to show evaluated value of a formula in the cell</param>
/// <returns></returns>
public static DataTable ToDataTable(ISheet sheet, bool firstRowAsHeader, bool showCalculatedFormulaValue)
{
DataTable dt = new DataTable(sheet.SheetName);

HSSFDataFormatter formatter = new HSSFDataFormatter();
var formulaEvaluator=sheet.Workbook.GetCreationHelper().CreateFormulaEvaluator();
for(int i = 0; i <= sheet.LastRowNum; i++)
{
var row=sheet.GetRow(i);
if(row == null)
{
continue;
}
if(i == 0) //don't support merged cell so far
{
if(firstRowAsHeader)
{
for(int j = 0; j < row.LastCellNum; j++)
{
var c = row.GetCell(j);
dt.Columns.Add(c == null ? "" : formatter.FormatCellValue(c));
}
continue;
}
}

DataRow dr = null!;
for(int j = 0; j < row.LastCellNum; j++)
{
if(j == 0)
{
for(int k = dt.Columns.Count; k < row.LastCellNum; k++)
{
dt.Columns.Add("Column " + k);
}
dr = dt.NewRow();
}
var c = row.GetCell(j);
if(c == null)
{
dr[j] = "";
continue;
}
switch(c.CellType)
{
case CellType.Numeric:
dr[j] = c.NumericCellValue;
break;
case CellType.String:
dr[j] = formatter.FormatCellValue(c);
break;
case CellType.Blank:
dr[j] = "";
break;
case CellType.Boolean:
dr[j] = c.BooleanCellValue;
break;
case CellType.Formula:
if(showCalculatedFormulaValue)
{
var cellValue = formulaEvaluator.Evaluate(c);
switch(cellValue.CellType)
{
case CellType.Numeric:
dr[j] = cellValue.NumberValue;
break;
case CellType.String:
dr[j] = cellValue.StringValue;
break;
case CellType.Boolean:
dr[j] = cellValue.BooleanValue;
break;
case CellType.Error:
if(c.ErrorCellValue== FormulaError.NULL.Code)
dr[j] = "#NULL!";
else if(c.ErrorCellValue == FormulaError.DIV0.Code)
dr[j] = "#DIV/0!";
else if(c.ErrorCellValue == FormulaError.NUM.Code)
dr[j] = "#NUM!";
else if(c.ErrorCellValue == FormulaError.NAME.Code)
dr[j] = "#NAME!";
break;
case CellType.Blank:
dr[j] = "";
break;
}
}
else
dr[j] = c.CellFormula;
break;
}
}
dt.Rows.Add(dr);
}
return dt;
}
}
}
14 changes: 13 additions & 1 deletion main/SS/Util/WorkbookUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NPOI.SS.UserModel;
using System;
using System.Data;
using System.Text;
using NPOI.SS.UserModel;

namespace NPOI.SS.Util
{
Expand Down Expand Up @@ -206,6 +207,17 @@ public static int GetNextActiveSheetDueToSheetHiding(IWorkbook wb, int sheetIx)
return sheetIx;
}
}
public static DataSet ToDataSet(IWorkbook workbook, bool firstRowAsHeader, bool showCalculatedFormulaValue = false)
{
DataSet ds = new DataSet();
for(int i = 0; i < workbook.NumberOfSheets; i++)
{
var sheet=workbook.GetSheetAt(0);
var dt=sheet.ToDataTable(firstRowAsHeader, showCalculatedFormulaValue);
ds.Tables.Add(dt);
}
return ds;
}
}

}
17 changes: 11 additions & 6 deletions ooxml/XSSF/Streaming/SXSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ limitations under the License.
==================================================================== */

using NPOI.HSSF.Util;
using NPOI.OpenXmlFormats.Spreadsheet;
using NPOI.SS;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using NPOI.SS;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using NPOI.OpenXmlFormats.Spreadsheet;

namespace NPOI.XSSF.Streaming
{
Expand Down Expand Up @@ -1678,5 +1679,9 @@ public NRowRange Rows
return new NRowRange(this, 0, this.Workbook.SpreadsheetVersion.MaxRows);
}
}
public DataTable ToDataTable(bool firstRowAsHeader = false, bool showCalculatedValue = false)
{
return SheetUtil.ToDataTable(this, firstRowAsHeader, showCalculatedValue);
}
}
}
19 changes: 12 additions & 7 deletions ooxml/XSSF/Streaming/SXSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ the License. You may obtain a copy of the License at
limitations under the License.
==================================================================== */

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using NPOI.OpenXml4Net.Util;
using NPOI.SS;
using NPOI.SS.Formula.UDF;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.Util;
using NPOI.XSSF.Model;
using NPOI.XSSF.UserModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

namespace NPOI.XSSF.Streaming
{
Expand Down Expand Up @@ -997,6 +997,11 @@ public bool IsDate1904()
return XssfWorkbook.IsDate1904();
}

public DataSet ToDataSet(bool firstRowAsHeader = false, bool showCalculatedValue = false)
{
return WorkbookUtil.ToDataSet(this, firstRowAsHeader, showCalculatedValue);
}

void IDisposable.Dispose()
{
this.Dispose();
Expand Down
6 changes: 6 additions & 0 deletions ooxml/XSSF/UserModel/XSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ limitations under the License.
using ST_EditAs = NPOI.OpenXmlFormats.Dml.Spreadsheet.ST_EditAs;
using System.Xml.Linq;
using System.Diagnostics;
using System.Data;

namespace NPOI.XSSF.UserModel
{
Expand Down Expand Up @@ -6626,6 +6627,11 @@ internal void OnSheetDelete() {
}
}

public DataTable ToDataTable(bool firstRowAsHeader = false, bool showCalculatedValue = false)
{
return SheetUtil.ToDataTable(this, firstRowAsHeader, showCalculatedValue);
}

public XSSFHeaderFooterProperties HeaderFooterProperties
{
get
Expand Down
7 changes: 6 additions & 1 deletion ooxml/XSSF/UserModel/XSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ limitations under the License.
using System.Collections;
using NPOI.OpenXml4Net.Exceptions;
using NPOI.SS;
using System.Globalization;
using System.Linq;
using NPOI.POIFS.FileSystem;
using System.Data;

namespace NPOI.XSSF.UserModel
{
Expand Down Expand Up @@ -2718,6 +2718,11 @@ public void Dispose()
{
this.Close();
}

public DataSet ToDataSet(bool firstRowAsHeader = false, bool showCalculatedValue = false)
{
return WorkbookUtil.ToDataSet(this, firstRowAsHeader, showCalculatedValue);
}
}
}

Expand Down
Loading