Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect csv/tsv encoding #30

Open
wants to merge 1 commit into
base: v1.4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ExcelMerge/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ internal class CsvReader
{
internal static IEnumerable<ExcelRow> Read(string path)
{
using (var sr = new StreamReader(path, Encoding.UTF8))
using (var stream = File.OpenRead(path))
{
var detector = new Ude.CharsetDetector();
detector.Feed(stream);
detector.DataEnd();
var encoding = detector.IsDone() ? Encoding.GetEncoding(detector.Charset) : Encoding.Default;
stream.Position = 0;
var sr = new StreamReader(stream, encoding);
var rowIndex = 0;
while (!sr.EndOfStream)
{
Expand All @@ -22,5 +28,6 @@ internal static IEnumerable<ExcelRow> Read(string path)
}
}
}

}
}
3 changes: 3 additions & 0 deletions ExcelMerge/ExcelMerge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Ude.NetStandard, Version=1.0.2.0, Culture=neutral, PublicKeyToken=103cb45fc06c90e4, processorArchitecture=MSIL">
<HintPath>..\packages\Ude.NetStandard.1.2.0\lib\net45\Ude.NetStandard.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CsvReader.cs" />
Expand Down
8 changes: 7 additions & 1 deletion ExcelMerge/TsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ public class TsvReader
{
internal static IEnumerable<ExcelRow> Read(string path)
{
using (var sr = new StreamReader(path, Encoding.UTF8))
using (var stream = File.OpenRead(path))
{
var detector = new Ude.CharsetDetector();
detector.Feed(stream);
detector.DataEnd();
var encoding = detector.IsDone() ? Encoding.GetEncoding(detector.Charset) : Encoding.Default;
stream.Position = 0;
var sr = new StreamReader(stream, encoding);
var rowIndex = 0;
while (!sr.EndOfStream)
{
Expand Down
1 change: 1 addition & 0 deletions ExcelMerge/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<package id="Diff4Net" version="1.1.2" targetFramework="net452" />
<package id="NPOI" version="2.3.0" targetFramework="net452" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net452" />
<package id="Ude.NetStandard" version="1.2.0" targetFramework="net452" />
</packages>