Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
neil3d committed Jun 10, 2018
2 parents f3940ca + 8ae29f6 commit 9ba3de9
Show file tree
Hide file tree
Showing 23 changed files with 263 additions and 734 deletions.
Binary file removed .DS_Store
Binary file not shown.
3 changes: 0 additions & 3 deletions .dockerignore

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,3 @@ UpgradeLog*.htm

# Microsoft Fakes
FakesAssemblies/
/.vs/excel2json/v15/Server/sqlite3
93 changes: 0 additions & 93 deletions ConvertBigInt.cs

This file was deleted.

5 changes: 0 additions & 5 deletions Dockerfile

This file was deleted.

172 changes: 51 additions & 121 deletions GUI/DataManager.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using Excel;
using System;
using System.Data;
using System.IO;
using System.Text;
using ExcelDataReader;

namespace excel2json.GUI {

/// <summary>
/// 为GUI模式提供的整体数据管理
/// </summary>
class DataManager
{

class DataManager {

// 数据导入设置
private Program.Options mOptions;
private Encoding mEncoding;
Expand All @@ -24,10 +23,8 @@ class DataManager
/// <summary>
/// 导出的Json文本
/// </summary>
public string JsonContext
{
get
{
public string JsonContext {
get {
if (mJson != null)
return mJson.context;
else
Expand All @@ -38,10 +35,8 @@ public string JsonContext
/// <summary>
/// 导出的SQL文本
/// </summary>
public string SQLContext
{
get
{
public string SQLContext {
get {
if (mSQL != null)
return mSQL.structSQL + mSQL.contentSQL;
else
Expand All @@ -52,10 +47,8 @@ public string SQLContext
/// <summary>
/// 导出的C#代码
/// </summary>
public string CSharpCode
{
get
{
public string CSharpCode {
get {
if (mCSharp != null)
return mCSharp.code;
else
Expand All @@ -67,10 +60,8 @@ public string CSharpCode
/// 保存Json
/// </summary>
/// <param name="filePath">保存路径</param>
public void saveJson(string filePath)
{
if (mJson != null)
{
public void saveJson(string filePath) {
if (mJson != null) {
mJson.SaveToFile(filePath, mEncoding);
}
}
Expand All @@ -79,10 +70,8 @@ public void saveJson(string filePath)
/// 保存SQL
/// </summary>
/// <param name="filePath">保存路径</param>
public void saveSQL(string filePath)
{
if (mSQL != null)
{
public void saveSQL(string filePath) {
if (mSQL != null) {
mSQL.SaveToFile(filePath, mEncoding);
}
}
Expand All @@ -91,10 +80,8 @@ public void saveSQL(string filePath)
/// 保存C#代码
/// </summary>
/// <param name="filePath">保存路径</param>
public void saveCode(string filePath)
{
if (mCSharp != null)
{
public void saveCode(string filePath) {
if (mCSharp != null) {
mCSharp.SaveToFile(filePath, mEncoding);
}
}
Expand All @@ -103,111 +90,54 @@ public void saveCode(string filePath)
/// 加载Excel文件
/// </summary>
/// <param name="options">导入设置</param>
public void loadExcel(Program.Options options)
{
public void loadExcel(Program.Options options) {
mOptions = options;
string excelPath = options.ExcelPath;
string excelName = Path.GetFileNameWithoutExtension(excelPath);
int header = options.HeaderRows;

// 加载Excel文件
using (FileStream excelFile = File.Open(excelPath, FileMode.Open, FileAccess.Read))
{
using (FileStream excelFile = File.Open(excelPath, FileMode.Open, FileAccess.Read)) {
// Reading from a OpenXml Excel file (2007 format; *.xlsx)
using (var excelReader = ExcelReaderFactory.CreateReader(excelFile))
{

// Reading from a OpenXml Excel file (2007 format; *.xlsx)
//IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelFile);

// The result of each spreadsheet will be created in the result.Tables
//excelReader.IsFirstRowAsColumnNames = true;
DataSet book = excelReader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = true,
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
// Gets or sets a value indicating the prefix of generated column names.
EmptyColumnNamePrefix = "Column",
// Gets or sets a value indicating whether to use a row from the
// data as column names.
UseHeaderRow = true,
// Gets or sets a callback to determine which row is the header row.
// Only called when UseHeaderRow = true.
ReadHeaderRow = (rowReader) =>
{
// F.ex skip the first row and use the 2nd row as column headers:
//rowReader.Read();
},
// Gets or sets a callback to determine whether to include the
// current row in the DataTable.
FilterRow = (rowReader) =>
{
return true;
},
// Gets or sets a callback to determine whether to include the specific
// column in the DataTable. Called once per column after reading the
// headers.
FilterColumn = (rowReader, columnIndex) =>
{
return true;
}
}
});
// 数据检测
if (book.Tables.Count < 1)
{
throw new Exception("Excel file is empty: " + excelPath);
}

// 取得数据
DataTable sheet = book.Tables[0];
if (sheet.Rows.Count <= 0)
{
throw new Exception("Excel Sheet is empty: " + excelPath);
}

//-- 确定编码
Encoding cd = new UTF8Encoding(false);
if (options.Encoding != "utf8-nobom")
{
foreach (EncodingInfo ei in Encoding.GetEncodings())
{
Encoding e = ei.GetEncoding();
if (e.HeaderName == options.Encoding)
{
cd = e;
break;
}
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelFile);

// The result of each spreadsheet will be created in the result.Tables
excelReader.IsFirstRowAsColumnNames = true;
DataSet book = excelReader.AsDataSet();

// 数据检测
if (book.Tables.Count < 1) {
throw new Exception("Excel file is empty: " + excelPath);
}

// 取得数据
DataTable sheet = book.Tables[0];
if (sheet.Rows.Count <= 0) {
throw new Exception("Excel Sheet is empty: " + excelPath);
}

//-- 确定编码
Encoding cd = new UTF8Encoding(false);
if (options.Encoding != "utf8-nobom") {
foreach (EncodingInfo ei in Encoding.GetEncodings()) {
Encoding e = ei.GetEncoding();
if (e.HeaderName == options.Encoding) {
cd = e;
break;
}
}
}
mEncoding = cd;

//-- 导出JSON文件
if (options.JsonPath != null && options.JsonPath.Length > 0)
{
JsonExporter exporter = new JsonExporter(sheet, header, options.Lowcase, options.ExportArray);
exporter.SaveToFile(options.JsonPath, cd);
}
//-- 导出JSON
mJson = new JsonExporter(sheet, header, options.Lowcase, options.ExportArray);

//-- 导出SQL文件
if (options.SQLPath != null && options.SQLPath.Length > 0)
{
SQLExporter exporter = new SQLExporter(excelName, sheet, header);
exporter.SaveToFile(options.SQLPath, cd);
}
//-- 导出SQL
mSQL = new SQLExporter(excelName, sheet, header);

//-- 生成C#定义文件
if (options.CSharpPath != null && options.CSharpPath.Length > 0)
{
CSDefineGenerator exporter = new CSDefineGenerator(excelName, sheet);
exporter.SaveToFile(options.CSharpPath, cd);
}
//-- 生成C#定义代码
mCSharp = new CSDefineGenerator(excelName, sheet);
}
}
}
}
}
Loading

0 comments on commit 9ba3de9

Please sign in to comment.