【新增】新增Excel2TextDiff工具项目,用于将excel转换为text或者直接将两个excel文件转换为text后再diff
parent
1d6698fac7
commit
10c2768f1a
|
|
@ -269,3 +269,4 @@ __pycache__/
|
|||
/config/output_data
|
||||
/config/output_lua
|
||||
/config/output_lua_without_test
|
||||
/src/Excel2TextDiff/Properties/launchSettings.json
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||
<PackageReference Include="ExcelDataReader" Version="3.6.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
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
|
||||
{
|
||||
class CommandLineOptions
|
||||
{
|
||||
[Option('p', "port", Required = false, HelpText = "listen port")]
|
||||
public int Port { get; set; } = 8899;
|
||||
}
|
||||
|
||||
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 ?? ""}]===");
|
||||
LoadRemainRows(reader, lines);
|
||||
} while (reader.NextResult());
|
||||
}
|
||||
File.WriteAllLines(outputTextFile, lines, System.Text.Encoding.UTF8);
|
||||
}
|
||||
|
||||
private void LoadRemainRows(IExcelDataReader reader, List<string> lines)
|
||||
{
|
||||
int rowIndex = 0;
|
||||
while (reader.Read())
|
||||
{
|
||||
++rowIndex; // 第一行是 meta ,跳过
|
||||
var row = new List<string>();
|
||||
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)
|
||||
{
|
||||
row = row.GetRange(0, lastNotEmptyIndex + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
row.Clear();
|
||||
}
|
||||
lines.Add(string.Join(',', row));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
using CommandLine;
|
||||
using CommandLine.Text;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Excel2TextDiff
|
||||
{
|
||||
class CommandLineOptions
|
||||
{
|
||||
[Option('t', SetName = "transform", HelpText = "transform excel to text file")]
|
||||
public bool IsTransform { get; set; }
|
||||
|
||||
[Option('d', SetName = "diff", HelpText = "transform and diff file")]
|
||||
public bool IsDiff { get; set; }
|
||||
|
||||
[Option('p', SetName = "diff", Required = false, HelpText = "3rd diff program. default TortoiseMerge")]
|
||||
public string DiffProgram { get; set; }
|
||||
|
||||
[Value(0)]
|
||||
public IList<string> Files { get; set; }
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//if (args.Length != 2 && args.Length != 3)
|
||||
//{
|
||||
// Console.WriteLine("Usage:");
|
||||
// Console.WriteLine("Excel2TextDiff <file1> <file2> [path_of_diff_program]");
|
||||
// return;
|
||||
//}
|
||||
|
||||
var options = ParseOptions(args);
|
||||
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
var writer = new Excel2TextWriter();
|
||||
|
||||
if (options.IsTransform)
|
||||
{
|
||||
if (options.Files.Count != 2)
|
||||
{
|
||||
Console.WriteLine("Usage: Excel2TextDiff -t <excel file> <text file>");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
writer.TransformToTextAndSave(options.Files[0], options.Files[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options.Files.Count != 2)
|
||||
{
|
||||
Console.WriteLine("Usage: Excel2TextDiff -d <excel file 1> <excel file 2> [-p <diff programe>]");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
var diffProgame = options.DiffProgram ?? "TortoiseMerge.exe";
|
||||
|
||||
var tempTxt1 = Path.GetTempFileName();
|
||||
writer.TransformToTextAndSave(options.Files[0], tempTxt1);
|
||||
|
||||
var tempTxt2 = Path.GetTempFileName();
|
||||
writer.TransformToTextAndSave(options.Files[1], tempTxt2);
|
||||
|
||||
string arg1 = $"/base:{tempTxt1.Replace('\\', '/')}";
|
||||
string arg2 = $"/mine:{tempTxt2.Replace('\\', '/')}";
|
||||
Console.WriteLine(" {0} {1}", arg1, arg2);
|
||||
System.Diagnostics.Process.Start(diffProgame, new string[] { arg1, arg2 });
|
||||
}
|
||||
}
|
||||
|
||||
private static CommandLineOptions ParseOptions(String[] args)
|
||||
{
|
||||
var helpWriter = new StringWriter();
|
||||
var parser = new Parser(ps =>
|
||||
{
|
||||
ps.HelpWriter = helpWriter;
|
||||
});
|
||||
|
||||
var result = parser.ParseArguments<CommandLineOptions>(args);
|
||||
if (result.Tag == ParserResultType.NotParsed)
|
||||
{
|
||||
Console.Error.WriteLine(helpWriter.ToString());
|
||||
Environment.Exit(1);
|
||||
}
|
||||
return ((Parsed<CommandLineOptions>)result).Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Luban.Job.Proto", "Luban.Jo
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Luban.Job.Db", "Luban.Job.Db\Luban.Job.Db.csproj", "{7467AC15-C61F-4C56-942F-18EAEA902C58}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Excel2TextDiff", "Excel2TextDiff\Excel2TextDiff.csproj", "{9477226F-469E-458F-A3AD-9115D777A65A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -57,6 +59,10 @@ Global
|
|||
{7467AC15-C61F-4C56-942F-18EAEA902C58}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7467AC15-C61F-4C56-942F-18EAEA902C58}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7467AC15-C61F-4C56-942F-18EAEA902C58}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9477226F-469E-458F-A3AD-9115D777A65A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9477226F-469E-458F-A3AD-9115D777A65A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9477226F-469E-458F-A3AD-9115D777A65A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9477226F-469E-458F-A3AD-9115D777A65A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
|
|||
Loading…
Reference in New Issue