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的更多相关文章
随机推荐
- 【转】JAVA输出内容打印到TXT以及不同系统中如何换行
JAVA输出内容打印到TXT以及不同系统中如何换行 http://xiyang.09.blog.163.com/blog/static/59827615201172552755293/ 2011-08 ...
- Selenium2.0+TestNG+Ant+Jenkins自动化测试浅尝
当前常用自动化测试工具 Web自动化测试工具:QTP .selenium等 性能自动化测试工具:loadrunner.jmeter等 接口自动化测试工具:SoapUI.postman等 手机自动化测试 ...
- android--系统路径获取
Environment 常用方法: * 方法:getDataDirectory()解释:返回 File ,获取 Android 数据目录.* 方法:getDownloadCacheDirectory( ...
- Linux学习常用命令大全
Linux知识大全 转载须说明出处,整理不易 一.常用的linux命令 1.2 ls 命令说明 1.3 ls 通配符的使用 2.切换目录cd命令 3.创建和删除文件操作 4.移动和拷贝文件 4.3.m ...
- 洛谷P2025 脑力大人之监听电话
题目描述 话说埃菲尔铁塔小区的房子只有一栋,且只有一层,其中每一家都装有一个监听器,具体地,如果编号为第i家的人给编号第\(j\)家的人打了电话,\(i \leq j\),当然,也会有些人无聊地自己给 ...
- ELK系列(4) - Elasticsearch cannot write xcontent for unknown value of type class java.math.BigDecimal
问题与分析 在使用Elasticsearch进行index数据时,发现报错如下: java.lang.IllegalArgumentException: cannot write xcontent f ...
- jquery——选项卡
下面是闭包做选项卡: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- oracle 容灾库日常维护 ,健康检查脚本 以及常见问题分析
select DEST_ID, APPLIED_SCN FROM v$archive_dest select * from v$dataguard_status; SELECT gvi.thread# ...
- 文本编辑简体中文专业版EmEditor Professional v12.0.8(12/27/2012更新)姓名+注册码
这是一个简单好用的文本编辑器,支持多种配置,自定义颜色.字体.工具栏.快捷键设置,可以调整行距,避免中文排列过于紧密,具有选择文本列块的功能(按ALT 键拖动鼠标),并允许无限撤消.重做,总之功能多多 ...
- asp.net 在IIS上配置出现的一些问题
1.可能会遇到一下图的错无.请求的内容似乎是脚本.因而将无法由静态文件处理程序来处理---大概的原因是应用程序池选择错误了.如第二幅图如此解决即可 解决方案如下两个图所示. 我遇到了以上的问题之后能也 ...