//获取用户打开的Excel文档路径
private stringkkk()
{
OpenFileDialog selectFile = new OpenFileDialog();
selectFile.Multiselect = false;
selectFile.Filter = "Excel Files(*.xls,*.xlsx)|*.xls;*.xlsx";
if (selectFile.ShowDialog() != DialogResult.OK)
return null;
string filePath = selectFile.FileName;
return filePath ;
}

/// <summary>
/// 创建Excel Table.
/// </summary>
/// <param name="colCount">列数</param>
/// <returns>DataTable</returns>
private System.Data.DataTable CreateExcelTable(int colCount)
{
System.Data.DataTable returnTable = new System.Data.DataTable();
for (int i = ; i <= colCount; i++)
returnTable.Columns.Add("col" + i.ToString(), typeof(string));
return returnTable;
} /// <summary>
/// 根据Excel路径,读取数据至DataSet.
/// </summary>
/// <param name="excelPath">Excel Path</param>
/// <returns>DataSet</returns>
public DataSet GetDataSetFromExcel(string excelPath)
{
DataSet resultDS = new DataSet();
Aspose.Cells.Workbook excelBook = new Aspose.Cells.Workbook();
excelBook.Open(excelPath); // get the rows and insert into dataset.
Aspose.Cells.Worksheet excelSheet = excelBook.Worksheets[];
if (!excelSheet.IsVisible)
{
DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏的Sheet:[{0}],请检查!", excelSheet.Name), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
return resultDS;
} Aspose.Cells.Cells excelValues = excelSheet.Cells;
foreach (Row r in excelValues.Rows)
{
if (r.IsHidden)
{
DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏行,行号:[{0}],请检查!", r.Index + ), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
return resultDS;
}
}
foreach (Column c in excelValues.Columns)
{
if (c.IsHidden)
{
DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏列,列号:[{0}],请检查!", (char)(c.Index + )), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
return resultDS;
}
}
int rowCount = excelValues.MaxRow;
int colCount = excelValues.MaxColumn; System.Data.DataTable excelTable = CreateExcelTable(colCount);
resultDS.Tables.Add(excelTable);
for (int i = ; i <= rowCount; i++)
{
//如果前5栏为空的话,则忽略添加新行。
if (Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == "")
continue;
DataRow row = excelTable.NewRow();
for (int j = ; j <= colCount; j++)
{
if (excelValues[i, j].Value == null)
row[j] = "";
else
row[j] = excelValues[i, j].Value.ToString();
}
excelTable.Rows.Add(row);
}
return resultDS;
} /// <summary>
/// 根据Excel路径,读取指定Sheet表数据至DataSet.
/// </summary>
/// <param name="excelPath">Excel Path</param>
/// <returns>DataSet</returns>
public DataSet GetDataSetFromExcel_SG3Nod(string excelPath,int x)
{
DataSet resultDS = new DataSet();
Aspose.Cells.Workbook excelBook = new Aspose.Cells.Workbook();
excelBook.Open(excelPath); // get the rows and insert into dataset.
Aspose.Cells.Worksheet excelSheet = excelBook.Worksheets[x];
if (!excelSheet.IsVisible)
{
DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏的Sheet:[{0}],请检查!", excelSheet.Name), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
return resultDS;
} Aspose.Cells.Cells excelValues = excelSheet.Cells;
foreach (Row r in excelValues.Rows)
{
if (r.IsHidden)
{
DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏行,行号:[{0}],请检查!", r.Index + ), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
return resultDS;
}
}
foreach (Column c in excelValues.Columns)
{
if (c.IsHidden)
{
DevExpress.XtraEditors.XtraMessageBox.Show(string.Format("Excel文件发现隐藏列,列号:[{0}],请检查!", (char)(c.Index + )), "P2解决方案", MessageBoxButtons.OK, MessageBoxIcon.Error);
return resultDS;
}
}
int rowCount = excelValues.MaxRow;
int colCount = excelValues.MaxColumn; System.Data.DataTable excelTable = CreateExcelTable(colCount);
resultDS.Tables.Add(excelTable);
for (int i = ; i <= rowCount; i++)
{
//如果前5栏为空的话,则忽略添加新行。
if (Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == ""
&& Convert.ToString(excelValues[i, ].Value) == "")
continue;
DataRow row = excelTable.NewRow();
for (int j = ; j <= colCount; j++)
{
if (excelValues[i, j].Value == null)
row[j] = "";
else
row[j] = excelValues[i, j].Value.ToString();
}
excelTable.Rows.Add(row);
}
return resultDS;
} /// <summary>
/// 读取Excel第一个Sheet至DataTable
/// </summary>
/// <param name="excelPath"></param>
/// <returns></returns>
public DataTable GetDataTableFromExcel(string excelPath)
{
DataSet resultDS = new DataSet();
Aspose.Cells.Workbook excelBook = new Aspose.Cells.Workbook(excelPath);
//excelBook.Open(excelPath); // get the rows and insert into dataset.
Aspose.Cells.Worksheet excelSheet = excelBook.Worksheets[];
Aspose.Cells.Cells excelValues = excelSheet.Cells;
int rowCount = excelValues.MaxRow + ;
int colCount = excelValues.MaxColumn + ;
return excelValues.ExportDataTable(, , rowCount, colCount); }

Excel2Dataset的更多相关文章

随机推荐

  1. product of大数据平台搭建------CM 和CDH安装

    一.安装说明 CM是由cloudera公司提供的大数据组件自动部署和监控管理工具,相应的和CDH是cloudera公司在开源的hadoop社区版的基础上做了商业化的封装的大数据平台. 采用离线安装模式 ...

  2. MD5Init-MD5Update-MD5Final

    MD5Init是一个初始化函数,初始化核心变量,装入标准的幻数 MD5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个函数由getMD5ofStr调用,调用之 ...

  3. js判断IP字符串是否正确

    //判断ip地址的合法性 function checkIP(value){ var exp=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0 ...

  4. C#中类和结构体

    结构体 类 自己的一些理解 首先结构中不能给字段赋值  而类可以 结构调用方法是  例如 People p1: 类的调用方法是  Book b =new Book(): 1.类能够实例化 而结构不可以 ...

  5. vue + eCharts 实现图表展示

    一.首先安装 eCharts 依赖 npm install echarts -S 二.main.js 引入 eCharts 依赖 2.1)在 main.js 中引入 import echarts fr ...

  6. Mysql索引优化2

    理想的索引 查询频繁 区分度高 长度小 尽量能覆盖常用查询字段 索引与排序 对于覆盖索引,直接在索引上查询时,就是有顺序的,using index 在innodb引擎中,沿着索引字段排序自然是有序的 ...

  7. 解读人:李琼,Metabolic profiling of tumors, sera and skeletal muscles from an orthotopic murine model of gastric cancer associated-cachexia(胃癌相关恶病质的原位小鼠模型中肿瘤,血清和骨骼肌的代谢谱分析)

    发表时间:(2019年4月) IF:3.950 单位: 厦门大学 厦门理工大学 物种:小鼠的肿瘤,血清和骨骼肌 技术:核磁共振代谢组学分析 一. 概述:(用精炼的语言描述文章的整体思路及结果) 本研究 ...

  8. 手写的select 下拉菜单

    我们在进行表单设计时,可能要用到select下拉选项控件,遗憾的是,IE浏览器默认的select控件外观非常丑陋,而且不能用样式来控制,不能在选项中添加图片等信息.今天我将通过实例来讲解如何用CSS和 ...

  9. git教程3-分支

    https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E7%AE%A1%E7%90%86 ...

  10. 对sass通过compass进行编译

    1.创建一个Compass项目:compass create myproject 其中myproject是项目名称.2.编写scss文件.3.编译成css文件:①:Compass的编译命令是:comp ...