/
Form1.cs
192 lines (174 loc) · 6.39 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Resources;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string path = "";
string fileName = "";
OleDbConnection objConn = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
comboBox1.Items.Clear();
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件夹";
dialog.Filter = "所有文件(*.*)|*.*";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
path = dialog.FileName;
}
label5.Text = path;
String[] sheetList = GetExcelSheetNames(path);
foreach (string str in sheetList)
{
comboBox1.Items.Add(str);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// read excel file
/// </summary>
/// <param name="sheetName"></param>
private void ReadExcel(string sheetName)
{
try
{
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select 标签,日文,中文 from [" + sheetName + "$]";
myCommand = new OleDbDataAdapter(strExcel, objConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
label2.Text="共"+ds.Tables[0].Rows.Count+"条记录";
}
finally
{
}
}
/// <summary>
/// 获取Excel工作薄中Sheet页(工作表)名集合
/// </summary>
/// <param name="excelFile">Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls</param>
/// <returns>Sheet页名称集合</returns>
private String[] GetExcelSheetNames(string fileName)
{
// 清理
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
System.Data.DataTable dt = null;
try
{
string connString = string.Empty;
string FileType = fileName.Substring(fileName.LastIndexOf("."));
if (FileType == ".xls")
connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
else//.xlsx
connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
// 创建连接对象
objConn = new OleDbConnection(connString);
// 打开数据库连接
objConn.Open();
// 得到包含数据架构的数据表
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// 添加工作表名称到字符串数组
foreach (DataRow row in dt.Rows)
{
string strSheetTableName = row["TABLE_NAME"].ToString();
//过滤无效SheetName
if (strSheetTableName.Contains("$") && strSheetTableName.Replace("'", "").EndsWith("$"))
{
excelSheets[i] = strSheetTableName.Substring(0, strSheetTableName.Length - 1);
}
i++;
}
return excelSheets;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
finally
{
if (dt != null)
{
dt.Dispose();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
ReadExcel(comboBox1.Text);
textBox1.Text = comboBox1.Text + "Resource.ja-jp.resx";
}
private void button3_Click(object sender, EventArgs e)
{
createResFile(textBox1.Text);
MessageBox.Show(textBox1.Text+"文件生成成功");
}
/// <summary>
/// create res file
/// </summary>
/// <param name="fileName"></param>
private void createResFile(string fileName)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
ResXResourceWriter writer = new ResXResourceWriter(fs);
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (!String.IsNullOrEmpty(dgvr.Cells[0].Value.ToString()))
{
//MessageBox.Show(dgvr.Cells[0].Value.ToString() + "=" + dgvr.Cells[1].Value.ToString());
writer.AddResource(dgvr.Cells[0].Value.ToString(), dgvr.Cells[1].Value.ToString());
}
}
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}