Skip to content

Commit efe12f6

Browse files
committed
提交代码
1 parent b661cbc commit efe12f6

File tree

9 files changed

+222
-1
lines changed

9 files changed

+222
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/bin
2+
/obj
3+
/*.user
4+
/.vs

Excel2TextDiff.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<None Remove="LICENSE" />
10+
<None Remove="README.md" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
15+
<PackageReference Include="ExcelDataReader" Version="3.6.0" />
16+
</ItemGroup>
17+
18+
</Project>

Excel2TextDiff.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31702.278
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Excel2TextDiff", "Excel2TextDiff.csproj", "{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {B761FE37-3592-45C9-919A-0EA4099214AF}
24+
EndGlobalSection
25+
EndGlobal

Excel2TextWriter.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using ExcelDataReader;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace Excel2TextDiff
6+
{
7+
class Excel2TextWriter
8+
{
9+
public void TransformToTextAndSave(string excelFile, string outputTextFile)
10+
{
11+
var lines = new List<string>();
12+
using var excelFileStream = new FileStream(excelFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
13+
string ext = Path.GetExtension(excelFile);
14+
using (var reader = ext != ".csv" ? ExcelReaderFactory.CreateReader(excelFileStream) : ExcelReaderFactory.CreateCsvReader(excelFileStream))
15+
{
16+
do
17+
{
18+
lines.Add($"===[{reader.Name ?? ""}]===");
19+
LoadRows(reader, lines);
20+
} while (reader.NextResult());
21+
}
22+
File.WriteAllLines(outputTextFile, lines, System.Text.Encoding.UTF8);
23+
}
24+
25+
private void LoadRows(IExcelDataReader reader, List<string> lines)
26+
{
27+
var row = new List<string>();
28+
while (reader.Read())
29+
{
30+
row.Clear();
31+
for (int i = 0, n = reader.FieldCount; i < n; i++)
32+
{
33+
object cell = reader.GetValue(i);
34+
row.Add(cell != null ? cell.ToString() : "");
35+
}
36+
// 只保留到最后一个非空白单元格
37+
int lastNotEmptyIndex = row.FindLastIndex(s => !string.IsNullOrEmpty(s));
38+
if (lastNotEmptyIndex >= 0)
39+
{
40+
lines.Add(string.Join(',', row.GetRange(0, lastNotEmptyIndex + 1)));
41+
}
42+
else
43+
{
44+
// 忽略空白行,没必要diff这个
45+
}
46+
}
47+
}
48+
}
49+
}

Program.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using CommandLine;
2+
using CommandLine.Text;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.IO;
7+
8+
namespace Excel2TextDiff
9+
{
10+
class CommandLineOptions
11+
{
12+
[Option('t', SetName = "transform", HelpText = "transform excel to text file")]
13+
public bool IsTransform { get; set; }
14+
15+
[Option('d', SetName = "diff", HelpText = "transform and diff file")]
16+
public bool IsDiff { get; set; }
17+
18+
[Option('p', SetName = "diff", Required = false, HelpText = "3rd diff program. default TortoiseMerge")]
19+
public string DiffProgram { get; set; }
20+
21+
[Option('f', SetName = "diff", Required = false, HelpText = "3rd diff program argument format. default is TortoiseMerge format:'/base:{0} /mine:{1}'")]
22+
public string DiffProgramArgumentFormat { get; set; }
23+
24+
[Value(0)]
25+
public IList<string> Files { get; set; }
26+
27+
[Usage()]
28+
public static IEnumerable<Example> Examples => new List<Example>
29+
{
30+
new Example("tranfrom to text", new CommandLineOptions { IsTransform = true, Files = new List<string>{"a.xlsx", "a.txt" } }),
31+
new Example("diff two excel file", new CommandLineOptions{ IsDiff = true, Files = new List<string>{"a.xlsx", "b.xlsx"}}),
32+
new Example("diff two excel file with TortoiseMerge", new CommandLineOptions{ IsDiff = true, DiffProgram = "TortoiseMerge",DiffProgramArgumentFormat = "/base:{0} /mine:{1}", Files = new List<string>{"a.xlsx", "b.xlsx"}}),
33+
};
34+
}
35+
36+
class Program
37+
{
38+
static void Main(string[] args)
39+
{
40+
var options = ParseOptions(args);
41+
42+
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
43+
var writer = new Excel2TextWriter();
44+
45+
if (options.IsTransform)
46+
{
47+
if (options.Files.Count != 2)
48+
{
49+
Console.WriteLine("Usage: Excel2TextDiff -t <excel file> <text file>");
50+
Environment.Exit(1);
51+
}
52+
53+
writer.TransformToTextAndSave(options.Files[0], options.Files[1]);
54+
}
55+
else
56+
{
57+
if (options.Files.Count != 2)
58+
{
59+
Console.WriteLine("Usage: Excel2TextDiff -d <excel file 1> <excel file 2> ");
60+
Environment.Exit(1);
61+
}
62+
63+
var diffProgame = options.DiffProgram ?? "TortoiseMerge.exe";
64+
65+
var tempTxt1 = Path.GetTempFileName();
66+
writer.TransformToTextAndSave(options.Files[0], tempTxt1);
67+
68+
var tempTxt2 = Path.GetTempFileName();
69+
writer.TransformToTextAndSave(options.Files[1], tempTxt2);
70+
71+
ProcessStartInfo startInfo = new ProcessStartInfo();
72+
startInfo.FileName = diffProgame;
73+
string argsFormation = options.DiffProgramArgumentFormat ?? "/base:{0} /mine:{1}";
74+
startInfo.Arguments = string.Format(argsFormation, tempTxt1, tempTxt2);
75+
Process.Start(startInfo);
76+
}
77+
}
78+
79+
private static CommandLineOptions ParseOptions(String[] args)
80+
{
81+
var helpWriter = new StringWriter();
82+
var parser = new Parser(ps =>
83+
{
84+
ps.HelpWriter = helpWriter;
85+
});
86+
87+
var result = parser.ParseArguments<CommandLineOptions>(args);
88+
if (result.Tag == ParserResultType.NotParsed)
89+
{
90+
Console.Error.WriteLine(helpWriter.ToString());
91+
Environment.Exit(1);
92+
}
93+
return ((Parsed<CommandLineOptions>)result).Value;
94+
}
95+
}
96+
}

Properties/launchSettings.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"profiles": {
3+
"Excel2TextDiff": {
4+
"commandName": "Project",
5+
"commandLineArgs": "-t D:\\workspace\\luban\\config\\Datas\\test\\full_type.xlsx d:\\full_type.transform.txt"
6+
},
7+
"diff": {
8+
"commandName": "Project",
9+
"commandLineArgs": "-d D:\\workspace\\luban\\config\\Datas\\test\\full_type.xlsx D:\\workspace\\luban\\config\\Datas\\test\\multi_level_title.xlsx"
10+
},
11+
"diff_3rd_format": {
12+
"commandName": "Project",
13+
"commandLineArgs": "-d -p notepad -f \"{0} \" D:\\workspace\\luban\\config\\Datas\\test\\full_type.xlsx D:\\workspace\\luban\\config\\Datas\\test\\multi_level_title.xlsx "
14+
},
15+
"help": {
16+
"commandName": "Project",
17+
"commandLineArgs": "--help"
18+
}
19+
}
20+
}

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# Excel2TextDiff
2-
将xlsx之类的文件转成text然后再调用diff工具对比变化,非常适合替换TortoiseGit,TortoiseSvn之类的默认diff命令。 convert excel file(xls,xlsx,xlm etc) to text then launch diff tool(TortoiseDiff) to show differences.
2+
3+
一个方便diff excel族(xls,xlsx,csv)文件的工具。将excel文件转为文本文件,然后再调用diff程序,极其方便直观地对比excel文件的变化。非常有用!
4+
5+
配置方式如图
6+
7+
![Excel2TextDiff](docs/images/a_1.jpg)
8+
9+
使用效果如图
10+
11+
![pipeline](docs/images/d_70.jpg)

docs/images/a_1.jpg

66.4 KB
Loading

docs/images/d_70.jpg

120 KB
Loading

0 commit comments

Comments
 (0)