25,980
社区成员
发帖
与我相关
我的任务
分享
using Aspose.Cells;
public DataSet ReadExcel()
{
try
{
DataSet ds = new DataSet();
Worksheet sheet = null;
Workbook book = new Workbook();
string excelPath = AppDomain.CurrentDomain.BaseDirectory + "Data\\html\\" +
HttpUtility.UrlDecode("国内进程.xlsx");
book.Open(excelPath);
for (int i = 0; i < book.Worksheets.Count; i++)
{
sheet = book.Worksheets[i];
DataTable dtTemp = new DataTable();
dtTemp.TableName = sheet.Name;
for (int x = 0; x < sheet.Cells.MaxDataRow + 1; x++)
{
DataRow dRow = null;
for (int y = 0; y < sheet.Cells.MaxDataColumn + 1; y++)
{
string value = sheet.Cells[x, y].StringValue.Trim();
if (x == 0)
{
DataColumn dCol = new DataColumn(value);
dtTemp.Columns.Add(dCol);
}
else
{
if (y == 0)
dRow = dtTemp.NewRow();
dRow[y] = value;
}
}
if (dRow != null)
{
dtTemp.Rows.Add(dRow);
}
}
ds.Tables.Add(dtTemp);
}
sheet = null;
book = null;
return ds;
}
catch (Exception ex)
{
LogHelper.WriteLog("ReadExcel方法异常", ex);
return null;
}
}

private static List<String[]> readerExcel(String path, String sheetName,
int minColumns) throws IOException, OpenXML4JException,
ParserConfigurationException, SAXException {
OPCPackage p = OPCPackage.open(path, PackageAccess.READ);
XLSXCovertCSVReader xlsx2csv = new XLSXCovertCSVReader(p, System.out,
sheetName, minColumns);
List<String[]> list = xlsx2csv.process();
p.close();
return list;
}