luban/src/Excel2TextDiff/Excel2TextWriter.cs

55 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using CommandLine;
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excel2TextDiff
{
class Excel2TextWriter
{
public void TransformToTextAndSave(string excelFile, string outputTextFile)
{
var lines = new List<string>();
using var excelFileStream = new FileStream(excelFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
string ext = Path.GetExtension(excelFile);
using (var reader = ext != ".csv" ? ExcelReaderFactory.CreateReader(excelFileStream) : ExcelReaderFactory.CreateCsvReader(excelFileStream))
{
do
{
lines.Add($"===[{reader.Name ?? ""}]===");
LoadRows(reader, lines);
} while (reader.NextResult());
}
File.WriteAllLines(outputTextFile, lines, System.Text.Encoding.UTF8);
}
private void LoadRows(IExcelDataReader reader, List<string> lines)
{
var row = new List<string>();
while (reader.Read())
{
row.Clear();
for (int i = 0, n = reader.FieldCount; i < n; i++)
{
object cell = reader.GetValue(i);
row.Add(cell != null ? cell.ToString() : "");
}
// 只保留到最后一个非空白单元格
int lastNotEmptyIndex = row.FindLastIndex(s => !string.IsNullOrEmpty(s));
if (lastNotEmptyIndex >= 0)
{
lines.Add(string.Join(',', row.GetRange(0, lastNotEmptyIndex + 1)));
}
else
{
// 忽略空白行没必要diff这个
}
}
}
}
}