首页 技术 正文
技术 2022年11月15日
0 收藏 802 点赞 4,769 浏览 4543 个字
//获取用户打开的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); }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848