一、首先去下载对应版本的DLL文件:https://s3.us-west-2.amazonaws.com/aspose.files/2023/08/09/Aspose.Cells_23.8.zip
二、解决方案引用
private void but_Click(object sender, EventArgs e)
{
#region 2读取文件
string fullname = "C:\\Users\\lin-yu\\111.xlsx";
string MO1 = "", WO = "", ITEM = "", ProcessCode = "", Line = "", Result = "", YYMMDD = "";//变量
string HHMM = "", WO_QTY = "", M_POWER = "", COUNT = "";
DataTable dt = Leo.Excel(fullname);
for (int i = 0; i < dt.Rows.Count; i++)
{
MO1 = dt.Rows[i][0].ToString().Trim().Replace("\"", "").Trim();
WO = dt.Rows[i][1].ToString().Trim().Replace("\"", "").Trim();
ITEM = dt.Rows[i][2].ToString().Trim().Replace("\"", "").Trim();
ProcessCode = dt.Rows[i][3].ToString().Trim().Replace("\"", "").Trim();
Line = dt.Rows[i][4].ToString().Trim().Replace("\"", "").Trim();
Result = dt.Rows[i][5].ToString().Trim();
YYMMDD = dt.Rows[i][6].ToString().Trim();
HHMM = dt.Rows[i][7].ToString().Trim();
WO_QTY = dt.Rows[i][8].ToString().Trim();
M_POWER = dt.Rows[i][9].ToString().Trim();
COUNT = dt.Rows[i][10].ToString().Trim();
}
#endregion
}
三、调用的方法:Leo.Excel(fullname);
public static DataTable Excel(string filepath)
{
//DataSet ds = null;
DataTable dt = null;
try
{
//打开文件
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(filepath);
//wb.Open(filepath);
//Aspose.Cells.Worksheets wss = wb.Worksheets;
Aspose.Cells.Worksheet ws = null;
Aspose.Cells.Cells cells = null;
if (wb.Worksheets.Count > 0)
{
// ds = new DataSet();
int rowIndex = 0;
int columnIndex = 0;
for (int i = 0; i < wb.Worksheets.Count; i++)
{
dt = new DataTable();
dt.TableName = "Table" + i.ToString();
//wb =wss[i];
cells = wb.Worksheets[i].Cells;
dt = cells.ExportDataTableAsString(rowIndex, columnIndex, cells.MaxDataRow + 1, cells.MaxColumn + 1, true);
//ds.Tables.Add(dt);
}
//wss.Clear();
ws = null;
//wss=null;
wb = null;
}
}
catch (Exception ex)
{
throw;
}
return dt;
}
#endregion