-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/bin | ||
/obj | ||
/*.user | ||
/.vs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="LICENSE" /> | ||
<None Remove="README.md" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommandLineParser" Version="2.8.0" /> | ||
<PackageReference Include="ExcelDataReader" Version="3.6.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.31702.278 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Excel2TextDiff", "Excel2TextDiff.csproj", "{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{95E742CA-CBB5-4B87-968D-C3F82F1EA2BA}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {B761FE37-3592-45C9-919A-0EA4099214AF} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using ExcelDataReader; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
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这个 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using CommandLine; | ||
using CommandLine.Text; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
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; } | ||
|
||
[Option('f', SetName = "diff", Required = false, HelpText = "3rd diff program argument format. default is TortoiseMerge format:'/base:{0} /mine:{1}'")] | ||
public string DiffProgramArgumentFormat { get; set; } | ||
|
||
[Value(0)] | ||
public IList<string> Files { get; set; } | ||
|
||
[Usage()] | ||
public static IEnumerable<Example> Examples => new List<Example> | ||
{ | ||
new Example("tranfrom to text", new CommandLineOptions { IsTransform = true, Files = new List<string>{"a.xlsx", "a.txt" } }), | ||
new Example("diff two excel file", new CommandLineOptions{ IsDiff = true, Files = new List<string>{"a.xlsx", "b.xlsx"}}), | ||
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"}}), | ||
}; | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
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> "); | ||
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); | ||
|
||
ProcessStartInfo startInfo = new ProcessStartInfo(); | ||
startInfo.FileName = diffProgame; | ||
string argsFormation = options.DiffProgramArgumentFormat ?? "/base:{0} /mine:{1}"; | ||
startInfo.Arguments = string.Format(argsFormation, tempTxt1, tempTxt2); | ||
Process.Start(startInfo); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"profiles": { | ||
"Excel2TextDiff": { | ||
"commandName": "Project", | ||
"commandLineArgs": "-t D:\\workspace\\luban\\config\\Datas\\test\\full_type.xlsx d:\\full_type.transform.txt" | ||
}, | ||
"diff": { | ||
"commandName": "Project", | ||
"commandLineArgs": "-d D:\\workspace\\luban\\config\\Datas\\test\\full_type.xlsx D:\\workspace\\luban\\config\\Datas\\test\\multi_level_title.xlsx" | ||
}, | ||
"diff_3rd_format": { | ||
"commandName": "Project", | ||
"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 " | ||
}, | ||
"help": { | ||
"commandName": "Project", | ||
"commandLineArgs": "--help" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
# Excel2TextDiff | ||
将xlsx之类的文件转成text然后再调用diff工具对比变化,非常适合替换TortoiseGit,TortoiseSvn之类的默认diff命令。 convert excel file(xls,xlsx,xlm etc) to text then launch diff tool(TortoiseDiff) to show differences. | ||
|
||
一个方便diff excel族(xls,xlsx,csv)文件的工具。将excel文件转为文本文件,然后再调用diff程序,极其方便直观地对比excel文件的变化。非常有用! | ||
|
||
配置方式如图 | ||
|
||
![Excel2TextDiff](docs/images/a_1.jpg) | ||
|
||
使用效果如图 | ||
|
||
![pipeline](docs/images/d_70.jpg) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.