public string excelParsing(string fullpath)
{
string data = "";
//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fullpath);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
//either collect data cell by cell or DO you job like insert to DB
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
data += xlRange.Cells[i, j].Value2.ToString();
}
}
return data;
}
3条答案
按热度按时间hs1ihplo1#
我想说的是,最简单的方法是让用户上传excel文件到您的MVC控制器,并使用
DocumentFormat.OpenXml
读取文件,然后将其保存到数据库或做任何你想要的数据。假设我们谈论的是关于XLSX文件,而不是XLS文件。文档格式.OpenXml可以在NuGet上找到:https://www.nuget.org/packages/DocumentFormat.OpenXml/
jxct1oxe2#
您的朋友是Microsoft. Office. Interop. Excel!它是一个很棒的库,可以做到这一点。只需添加对它和Microsoft Office 14. 0对象库的引用。
网上有很多这方面的指南
祝你好运
9o685dep3#
你可以试试这样的方法:
http://sforsuresh.in/how-to-read-an-excel-file-using-asp-net-mvc4/