我是C#和wpf的新手,但我试图从+100 xlsx文件中读取大量数据(abt.40000 x 34 cell)并将它们连接成一个文本文件。
我已经制作了一个小型的C#(wpf)应用程序来做到这一点。我看到一些随机的内存泄漏,有时当我运行它时,一切顺利,它需要的内存(根据Windows任务管理器)保持在1 GB以下,而其他时候它吃,几乎所有我的8 GB。
使用System.IO.File.ReadAllLines(pathtotextfilewithxlsxfilenames)
时发现了一种情况
而使用谨慎的例子(在代码中指定文件名)不会以同样的方式导致问题。 (我已经将带有xlsx-filenames的文本文件中的相同名称复制并粘贴到代码中(用", "
替换了标签)所以它应该是相同的...据我所知。)
该应用大致如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;
using System.Data.OleDb;
namespace WpfApps
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string s_path_to_list_of_xlsx_files;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string path = @"F:\...\xls_files\";
string[] fnlist = { "datafile1.xlsx", "datafile2.xlsx", ... "datafileN.xlsx" };
//string[] fnlist = System.IO.File.ReadAllLines(@"f:\...\xlsfiles.txt"); // It seems as if using this line, compared to the discreet line above causes memory leak.
DataTable measurementdata = new DataTable();
measurementdata.Columns.Add("C1", typeof(double));
measurementdata.Columns.Add("C2", typeof(double));
...
measurementdata.Columns.Add("C34", typeof(double));
using (StreamWriter sw = File.CreateText(path + "allresult.txt"))
{
bool firstCol = true;
foreach (DataColumn col in measurementdata.Columns)
{
if (!firstCol) sw.Write(", ");
sw.Write(col.ColumnName);
firstCol = false;
}
sw.WriteLine();
}
foreach (var fn in fnlist)
{
DataTable filedt = GetDataTableFromExcel(path + fn, false);
using (StreamWriter sw = File.AppendText(path + "allresult.txt"))
{
foreach (DataRow row in filedt.Rows)
{
bool firstCol = true;
foreach (DataColumn col in filedt.Columns)
{
if (!firstCol) sw.Write(", ");
sw.Write(row[col].ToString());
firstCol = false;
}
sw.WriteLine();
}
}
}
}
public static DataTable GetDataTableFromExcel(string path, bool hasHeader = true)
{
using (var pck = new OfficeOpenXml.ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets["Logged Data"];
int startrow = 72;
int endrow = 43271;
DataTable tbl = new DataTable();
foreach (var firstrowcell in ws.Cells[startrow - 1, 8, startrow - 1, 41])
{
tbl.Columns.Add(firstrowcell.Text);
}
for (int rowNum = startrow; rowNum <= endrow; rowNum++)
{
var wsRow = ws.Cells[rowNum, 8, rowNum, 41];
DataRow row = tbl.Rows.Add();
foreach (var cell in wsRow)
{
var cellvalue = cell.Value;
if (cellvalue == null) cellvalue = "0";
row[cell.Start.Column - 8] = cellvalue;
}
}
return tbl;
}
}
有人能看到明显的问题吗?