-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.cs
115 lines (94 loc) · 4.33 KB
/
replace.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
[ComVisible(true)]
public class WinForm : Form
{
public int Execute(string json)
{
int founds = 0;
Dictionary<string, string> dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
string input_1 = dic.ContainsKey("input_1") ? dic["input_1"] : "";
string input_2 = dic.ContainsKey("input_2") ? dic["input_2"] : "";
object input_3 = dic.ContainsKey("input_3") ? dic["input_3"] : "";
object missing = Type.Missing;
Document document = AutoWordAddIn.Plugin.currentDoc;
Microsoft.Office.Interop.Word.Range rng = document.Content;
rng.Find.ClearFormatting();
rng.Find.Forward = true;
rng.Find.Text = input_1;
rng.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
while (rng.Find.Found)
{
founds++;
if (input_3.ToString().Length > 0)
{
document.Comments.Add(document.Range(rng.Start, rng.End), ref input_3);
}
rng.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
}
rng = document.Content;
rng.Find.ClearFormatting();
rng.Find.Forward = true;
rng.Find.Text = input_1;
rng.Find.Replacement.ClearFormatting();
rng.Find.Replacement.Text = input_2;
object replaceAll = WdReplace.wdReplaceAll;
rng.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
return founds;
}
public string GetData()
{
string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
dataPath += "\\BlueAmber\\AutoWordAddIn";
string dataFile = dataPath + "\\data-replace.json";
using (System.IO.FileStream fs = new System.IO.FileStream(dataFile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
byte[] fsb = new byte[fs.Length];
fs.Read(fsb, 0, fsb.Length);
return Encoding.UTF8.GetString(fsb);
}
}
public string SetData(string json)
{
string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
dataPath += "\\BlueAmber\\AutoWordAddIn";
string dataFile = dataPath + "\\data-replace.json";
System.IO.File.WriteAllText(dataFile, json);
return "已保存至 " + dataFile;
}
public string GetExcel()
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Excel 文件|*.xls;*.xlsx";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(fileDialog.FileName);
Microsoft.Office.Interop.Excel.Worksheet ws = wb.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Range rg = ws.UsedRange;
List<Dictionary<string, object>> dics = new List<Dictionary<string, object>>();
for (int i = 1; i <= rg.Rows.Count; i++)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
for (int j = 1; j <= rg.Columns.Count; j++)
{
Microsoft.Office.Interop.Excel.Range cell = rg.Cells[i, j] as Microsoft.Office.Interop.Excel.Range;
dic["input_" + j.ToString()] = cell.Text;
}
dics.Add(dic);
}
wb.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
return JsonConvert.SerializeObject(dics);
}
return "[]";
}
}