You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.4 KiB
78 lines
2.4 KiB
using FastExcel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ExcelNet
|
|
{
|
|
public class ExcelReader
|
|
{
|
|
|
|
public List<string> Get_ExcelSheets(string filename)
|
|
|
|
{
|
|
Worksheet sheet = new Worksheet();
|
|
var inputFile = new FileInfo(filename);
|
|
FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true);
|
|
var worksheets = new List<string>();
|
|
for (int i = 0; i < fastExcel.Worksheets.Count(); i++)
|
|
{
|
|
worksheets.Add(fastExcel.Worksheets[i].Name);
|
|
}
|
|
return worksheets;
|
|
|
|
}
|
|
public void Excel_To_Datatabable(string filename, string sheet, bool includinheaders, ref System.Data.DataTable data)
|
|
{
|
|
Worksheet worksheet = null;
|
|
data.Rows.Clear();
|
|
data.Columns.Clear();
|
|
|
|
|
|
var inputFile = new FileInfo(filename);
|
|
|
|
using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true))
|
|
{
|
|
// Read the rows using worksheet name
|
|
// worksheet = fastExcel.Read(sheet);
|
|
|
|
// Read the rows using the worksheet index
|
|
// Worksheet indexes are start at 1 not 0
|
|
// This method is slightly faster to find the underlying file (so slight you probably wouldn't notice)
|
|
worksheet = fastExcel.Read(sheet);
|
|
|
|
try
|
|
{
|
|
|
|
var rows = worksheet.Rows.ToArray();
|
|
var cells = rows[0].Cells.ToArray();
|
|
if (includinheaders)
|
|
{
|
|
for (int j = 0; j < cells.Length; j++)
|
|
{
|
|
data.Columns.Add(cells[j].Value.ToString());
|
|
}
|
|
}
|
|
for (int i = 1; i < rows.Length; i++)
|
|
{
|
|
cells = rows[i].Cells.ToArray();
|
|
|
|
DataRow dr = data.NewRow();
|
|
|
|
for (int j = 0; j < cells.Length; j++)
|
|
{
|
|
dr[j] = cells[j].Value.ToString();
|
|
}
|
|
data.Rows.Add(dr);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|
|
}
|