如何从xls文件而不是xlsx中读取数据?(Dart)[关闭]

sdnqo3pr  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(190)

**已关闭。**此问题是在寻求有关书籍、工具、软件库等内容的建议。它不符合Stack Overflow guidelines。它目前不接受回答。

我们不允许提问以寻求书籍、工具、软件库等方面的建议。您可以编辑问题,以便使用事实和引文回答。
19天前关闭
Improve this question
我需要提取数据从旧的软件,只通过导出他的信息.xls文件 (当然这是旧的软件) 我想自动化的过程和存储这些数据,我可以读取.xlsx文件,但没有库,提供这样的事情.xls
我能把它转换成flutter吗?我能读到吗?
我需要提取值,而不是格式或其他任何东西,只需要提取行和列中的数据。

  • 谢谢,任何输入都将受到欢迎。
0lvr5msh

0lvr5msh1#

使用https://pub.dev/packages/excel

方法

  1. Future<ExcelData> fetchAndReadExcelDirectly() async {
  2. final response = await http
  3. .get(Uri.parse('https://go.microsoft.com/fwlink/?LinkID=521962'));
  4. if (response.statusCode == 200) {
  5. // The request was successful
  6. final bytes = response.bodyBytes;
  7. // Read the Excel file
  8. var excel = Excel.decodeBytes(bytes);
  9. var firstTable = excel.tables.keys.first;
  10. var firstRow = excel.tables[firstTable]?.rows[1];
  11. //.rows[1]; you can access all rows and get their data.
  12. if (firstRow == null) {
  13. throw Exception('The excel file is empty');
  14. }
  15. print(ExcelData.fromExcelRow(firstRow));
  16. return ExcelData.fromExcelRow(firstRow);
  17. } else {
  18. // The request failed, throw an exception
  19. throw Exception('Failed to load Excel file');
  20. }
  21. }

字符串

模型样本

  1. import 'package:excel/excel.dart';
  2. class ExcelData {
  3. final String segment;
  4. final String country;
  5. final String product;
  6. final String discountBand;
  7. final String unitsSold;
  8. final String manufacturingPrice;
  9. final String salePrice;
  10. final String grossSales;
  11. final String discounts;
  12. final String sales;
  13. final String cogs;
  14. final String profit;
  15. final String date;
  16. final String monthNumber;
  17. final String monthName;
  18. final String year;
  19. // You need to organize the data according to their data types and convert them to the relevant types instead of toString below.
  20. ExcelData({
  21. required this.segment,
  22. required this.country,
  23. required this.product,
  24. required this.discountBand,
  25. required this.unitsSold,
  26. required this.manufacturingPrice,
  27. required this.salePrice,
  28. required this.grossSales,
  29. required this.discounts,
  30. required this.sales,
  31. required this.cogs,
  32. required this.profit,
  33. required this.date,
  34. required this.monthNumber,
  35. required this.monthName,
  36. required this.year,
  37. });
  38. // This function works similar to a 'fromJson' method
  39. factory ExcelData.fromExcelRow(List<dynamic> row) {
  40. return ExcelData(
  41. segment: (row[0] as Data).value.toString(),
  42. country: (row[1] as Data).value.toString(),
  43. product: (row[2] as Data).value.toString(),
  44. discountBand: (row[3] as Data).value.toString(),
  45. unitsSold: (row[4] as Data).value.toString(),
  46. manufacturingPrice: (row[5] as Data).value.toString(),
  47. salePrice: (row[6] as Data).value.toString(),
  48. grossSales: (row[7] as Data).value.toString(),
  49. discounts: (row[8] as Data).value.toString(),
  50. sales: (row[9] as Data).value.toString(),
  51. cogs: (row[10] as Data).value.toString(),
  52. profit: (row[11] as Data).value.toString(),
  53. date: (row[12] as Data).value.toString(),
  54. monthNumber: (row[13] as Data).value.toString(),
  55. monthName: (row[14] as Data).value.toString(),
  56. year: (row[15] as Data).value.toString(),
  57. // Please organize the model according to variable types.
  58. );
  59. }
  60. @override
  61. String toString() {
  62. return 'ExcelData{segment: $segment, country: $country, product: $product, discountBand: $discountBand, unitsSold: $unitsSold, manufacturingPrice: $manufacturingPrice, salePrice: $salePrice, grossSales: $grossSales, discounts: $discounts, sales: $sales, cogs: $cogs, profit: $profit, date: $date, monthNumber: $monthNumber, monthName: $monthName, year: $year}';
  63. }
  64. }


如果传入文件是xls:
我们需要使用ASP.NET将文件转换为xlsx。(不幸的是,您无法使用dart进行转换。)
Asp.net核心控制器代码:

  1. [Route("api/[controller]")]
  2. [ApiController]
  3. public class ExcelController : ControllerBase
  4. {
  5. [HttpPost]
  6. public IActionResult Post([FromBody] LinkRequest request)
  7. {
  8. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  9. using (var client = new WebClient())
  10. {
  11. // Download the file data from the provided URL
  12. var data = client.DownloadData(request.Link);
  13. using (var reader = ExcelReaderFactory.CreateReader(new MemoryStream(data)))
  14. {
  15. var dataSet = reader.AsDataSet();
  16. var dataTable = dataSet.Tables[0];
  17. // Convert the DataTable to a list of dynamic objects
  18. var rows = dataTable.AsEnumerable().Skip(1) // Skip the header row
  19. .Select(row => new
  20. {
  21. Segment = row["Column0"],
  22. Country = row["Column1"],
  23. Product = row["Column2"],
  24. DiscountBand = row["Column3"],
  25. UnitsSold = row["Column4"],
  26. ManufacturingPrice = row["Column5"],
  27. SalePrice = row["Column6"],
  28. GrossSales = row["Column7"],
  29. Discounts = row["Column8"],
  30. Sales = row["Column9"],
  31. COGS = row["Column10"],
  32. Profit = row["Column11"],
  33. Date = row["Column12"],
  34. MonthNumber = row["Column13"],
  35. MonthName = row["Column14"],
  36. Year = row["Column15"]
  37. })
  38. .ToList();
  39. // Return the list of rows as a JSON response
  40. return new JsonResult(rows);
  41. }
  42. }
  43. }
  44. }
  45. public class LinkRequest
  46. {
  47. public string Link { get; set; }
  48. }


并请求样本


的数据
asp.net软件包


展开查看全部

相关问题