Ejemplo n.º 1
0
        public static IEnumerable <SimpleTable> ToTex(this string xls)
        {
            FileStream       stream      = File.Open(xls, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = null;

            if (Path.GetExtension(xls) == ".xls")
            {
                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            }
            else if (Path.GetExtension(xls) == ".xlsx")
            {
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }
            else
            {
                throw new ArgumentException("Not Support Format: ");
            }
            DataSet dataSet = excelReader.AsDataSet();

            foreach (DataTable dataTable in dataSet.Tables)
            {
                var table = new SimpleTable {
                    Name  = dataTable.TableName,
                    Value = dataTable.ToTex()
                };

                yield return(table);
            }

            excelReader.Close();
        }
Ejemplo n.º 2
0
        public static SimpleTable ToJson(this string xls, string sheet)
        {
            FileStream       stream      = File.Open(xls, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = null;

            if (Path.GetExtension(xls) == ".xls")
            {
                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            }
            else if (Path.GetExtension(xls) == ".xlsx")
            {
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }
            else
            {
                throw new ArgumentException("Not Support Format: ");
            }
            DataSet   dataSet   = excelReader.AsDataSet();
            DataTable dataTable = dataSet.Tables[sheet];

            var table = new SimpleTable {
                Name  = dataTable.TableName,
                Value = dataTable.ToJson()
            };

            excelReader.Close();

            return(table);
        }