Excel2Dataset
//获取用户打开的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的更多相关文章
随机推荐
- JavaScript中函数作为值
function myfunc() { // .. } 这是个函数,这样理解, myfunc只是外层作用域的一个变量,指向刚刚声明的function. 也就是说,function本身就是一个值, 就像 ...
- HN669打包工具--游戏对接
一. 将游戏工程拖入到工具的HN669Ploy目录下,如下图: 二. xCode打开游戏工程,将Core目录下的HN669SDKCore工程添加入游戏工程,并引用库,如图: 三.调用API 1.A ...
- Haproxy+Keepalived高可用配置
基本实验 参考文档 博文地址 环境拓扑 下面使我们要实现的负载均衡集群图示 主节点地址: 92.0.0.11 从节点地址: 92.0.0.12 共享虚拟地址:92.0.0.8 下面是负载均衡集群可能出 ...
- 上传文件到linux乱码问题
由于linux系统编码一般设置为utf-8,而中文windows下通常默认编码是gbk,因此经常需要将文件名或文件内容编码进行转换,文件名编码转换软件:convmv yum install convm ...
- tomcat memecached session 共享同步问题的解决
事件缘由:一个主项目“图说美物”,另外一个子功能是品牌商的入驻功能,是跟主项目分开的项目,为了共享登录的用户信息,而实现session共享,俩个tomcat,一个tomcat6,一个tomcat7 w ...
- vue中一些常见错误
一:在抽取路由模块时路径没有更改过来 二:跨域的问题
- iphone手机QQ浏览器到底部继续上滑出现黑色背景盖住position:fixed的页面元素的问题
真的是试了网上很多种都不行, 最后我html{overflow-x:hidden;}加了这个,去掉就可以了~删除html{overflow-x:hidden;} body{overflow-x:hid ...
- Ubuntu16.04双网卡绑定
服务器经常有多个网卡,为了保证网络冗余性,一个网卡出现故障时,不导致网络服务中断,可以懂多网卡网卡绑定来解决此问题. 环境: 系统:Ubuntu16.04 网卡:em1 em2 ip:192.168. ...
- SPA 介绍
SQL 性能分析器(SPA)工具概览 作为 Oracle Real Application Testing 选件/特性,这篇文章将提供一个关于 SQL 性能分析器(SPA)工具的简要概览.这是此系列的 ...
- Storm概念学习系列之Storm与Hadoop的角色和组件比较
不多说,直接上干货! Storm与Hadoop的角色和组件比较 Storm 集群和 Hadoop 集群表面上看很类似.但是 Hadoop 上运行的是 MapReduce 作业,而在 Storm 上运行 ...