我想「乾淨」的CSV文件:刪除空行和列從一個CSV文件 - C#
- 刪除空行
- 刪除空列
的行或列它們不是完全空的,例如: 「」,「」,「」,「」,「」,「」,「」,「」,「」,「」,「」,「」,「」 ,「」, (成行) 或 「」,「」,「」,「」,「」,「」,「」,「」,「」,「」, ) OR
「」,
「」,
「」,
「」,
「」,
「」,
「 「,
(以列的形式)
這些行或列可以位於CSV文件的任何位置。
我有什麼至今:
private void button1_Click(object sender, EventArgs e)
{
string sourceFile = @"XXXXX.xlsx";
string worksheetName = "Sample";
string targetFile = @"C:\Users\xxxx\xls_test\XXXX.csv";
// Creates the CSV file based on the XLS file
ExcelToCSVCoversion(sourceFile, worksheetName, targetFile);
// Manipulate the CSV: Clean empty rows
DeleteEmptyRoadFromCSV(targetFile);
}
static void ExcelToCSVCoversion(string sourceFile, string worksheetName,
string targetFile)
{
string connectionString = @"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile
+ @";Extended Properties=""Excel 12.0 Xml;HDR=YES""";
OleDbConnection connection = null;
StreamWriter writer = null;
OleDbCommand command = null;
OleDbDataAdapter dataAdapter = null;
try
{
// Represents an open connection to a data source.
connection = new OleDbConnection(connectionString);
connection.Open();
// Represents a SQL statement or stored procedure to execute
// against a data source.
command = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]",
connection);
// Specifies how a command string is interpreted.
command.CommandType = CommandType.Text;
// Implements a TextWriter for writing characters to the output stream
// in a particular encoding.
writer = new StreamWriter(targetFile);
// Represents a set of data commands and a database connection that are
// used to fill the DataSet and update the data source.
dataAdapter = new OleDbDataAdapter(command);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
for (int row = 0; row < dataTable.Rows.Count; row++)
{
string rowString = "";
for (int column = 0; column < dataTable.Columns.Count; column++)
{
rowString += "\"" + dataTable.Rows[row][column].ToString() + "\",";
}
writer.WriteLine(rowString);
}
Console.WriteLine();
Console.WriteLine("The excel file " + sourceFile + " has been converted " +
"into " + targetFile + " (CSV format).");
Console.WriteLine();
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
Console.ReadLine();
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
connection.Dispose();
command.Dispose();
dataAdapter.Dispose();
writer.Close();
writer.Dispose();
}
}
static void DeleteEmptyRoadFromCSV(string fileName)
{
//string nonEmptyLines = @"XXXX.csv";
var nonEmptyLines = File.ReadAllLines(fileName)
.Where(x => !x.Split(',')
.Take(2)
.Any(cell => string.IsNullOrWhiteSpace(cell))
// use `All` if you want to ignore only if both columns are empty.
).ToList();
File.WriteAllLines(fileName, nonEmptyLines);
}
最後,我試圖從用意念: Remove Blank rows from csv c#。但是我的輸出完全沒有變化。
歡迎任何幫助!
謝謝。
你爲什麼要重新發明車輪?當你可以使用文本文件解析器時,這是很多工作,而解析器也會更健壯。 –
另外,'File.ReadAllLines'可能是危險的,除非你確定你正在處理小文件。 – GibralterTop
Linq在這裏可能會有所幫助,您可以跳過空行/列。它也可能有助於清理你的代碼。 –