Skip to content

Commit 0e81680

Browse files
committed
固定行高,优化显示
1 parent e5920b8 commit 0e81680

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

ExcelMerge.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25123.0
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28010.2003
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelMerge", "ExcelMerge\ExcelMerge.csproj", "{09AA7F86-7EF6-44D4-98D9-916D4E2FC258}"
77
EndProject
88
Global
9+
GlobalSection(Performance) = preSolution
10+
HasPerformanceSessions = true
11+
EndGlobalSection
912
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1013
Debug|Any CPU = Debug|Any CPU
1114
Release|Any CPU = Release|Any CPU
@@ -19,4 +22,7 @@ Global
1922
GlobalSection(SolutionProperties) = preSolution
2023
HideSolutionNode = FALSE
2124
EndGlobalSection
25+
GlobalSection(ExtensibilityGlobals) = postSolution
26+
SolutionGuid = {F2077F71-FEEB-4300-92BD-022CE4466E31}
27+
EndGlobalSection
2228
EndGlobal

ExcelMerge/CellTemplateSelector.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public override System.Windows.DataTemplate SelectTemplate(object item, System.W
109109

110110
} else {
111111
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBox));
112-
textBlock.SetValue(TextBox.TextProperty, rowdata.data[Binder].value);
112+
if (rowdata.data.ContainsKey(Binder)) {
113+
textBlock.SetValue(TextBox.TextProperty, rowdata.data[Binder].value);
114+
}
113115
return new DataTemplate() { VisualTree = textBlock };
114116
}
115117

ExcelMerge/ExcelGridControl.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
BeginningEdit="ExcelGrid_BeginningEdit"
2323
EnableColumnVirtualization="True" EnableRowVirtualization="True"
2424
VirtualizingPanel.VirtualizationMode="Recycling"
25+
VirtualizingPanel.IsVirtualizing="True"
2526
ScrollViewer.ScrollChanged ="ExcelGrid_ScrollChanged" SelectionChanged="ExcelGrid_SelectionChanged" SelectedCellsChanged="ExcelGrid_SelectedCellsChanged">
2627
<DataGrid.ContextMenu>
2728
<ContextMenu Name="CtxMenu">

ExcelMerge/ExcelGridControl.xaml.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public void RefreshData() {
163163

164164
if (headershow == null || headerkey == null) return;
165165

166+
int linecount = 0;
166167
for (int i = 0; i < columnCount; ++i) {
167168
var cellshow = headershow.GetCell(i);
168169
var cellkey = headerkey.GetCell(i);
@@ -179,6 +180,7 @@ public void RefreshData() {
179180
}
180181
// 第二行+第三行,合起来作为key
181182
var encodestr = System.Uri.EscapeDataString(strkey) + "_" + i;// + System.Uri.EscapeDataString(str);
183+
linecount = Math.Max(linecount, strshow.Count((c) => { return c == '\n'; })+1);
182184

183185
var tc = new DataGridTemplateColumn();
184186
tc.Header = strshow;
@@ -189,9 +191,9 @@ public void RefreshData() {
189191

190192
headerStr[i] = encodestr;
191193
}
194+
ExcelGrid.ColumnHeaderHeight = linecount * 25;
192195
}
193196
else {
194-
195197
//AddPrefixRowID();
196198

197199
for (int i = 0; i < columnCount; ++i) {
@@ -206,6 +208,7 @@ public void RefreshData() {
206208

207209
headerStr[i] = str;
208210
}
211+
ExcelGrid.ColumnHeaderHeight = 25;
209212
}
210213

211214
if (needChangeHead) {
@@ -224,9 +227,10 @@ public void RefreshData() {
224227

225228
for (int i = 0; i < columnCount; ++i) {
226229
var cell = row.GetCell(i);
227-
data.data[headerStr[i]] = new CellData() { value = Util.GetCellValue(cell), cell = cell };
230+
var value = Util.GetCellValue(cell);
231+
data.data[headerStr[i]] = new CellData() { value = value, cell = cell };
228232
}
229-
233+
data.maxLineCount = status.DiffMaxLineCount[j];
230234
datas.Add(data);
231235
data_maps[data.rowId] = data;
232236
}
@@ -253,16 +257,17 @@ public void RefreshData() {
253257
data.data["rowid"] = new CellData() { value = (rowid+1).ToString() };
254258
for (int i = 0; i < columnCount; ++i) {
255259
var cell = row != null ? row.GetCell(i):null;
256-
data.data[headerStr[i]] = new CellData() { value = Util.GetCellValue(cell), cell = cell};
260+
var value = Util.GetCellValue(cell);
261+
data.data[headerStr[i]] = new CellData() { value = value, cell = cell};
257262
}
263+
data.maxLineCount = status.DiffMaxLineCount[j];
258264

259265
datas.Add(data);
260266
data_maps[data.rowId] = data;
261267
}
262268
}
263269
}
264270
ExcelGrid.ItemsSource = datas;
265-
//ExcelGrid.DataContext = datas;
266271

267272
CtxMenu.Items.Clear();
268273
var item = new MenuItem();
@@ -314,6 +319,7 @@ private void ExcelGrid_LoadingRow(object sender, DataGridRowEventArgs e) {
314319

315320
if (item != null) {
316321
row.Header = (item.rowId+1).ToString();
322+
row.Height = item.maxLineCount * 17;
317323
}
318324
//row.Header = ite
319325
}

ExcelMerge/MainWindow.xaml.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ SheetDiffStatus DiffSheet(ISheet src, ISheet dst, SheetDiffStatus status = null)
348348
status.rowID2DiffMap2 = new Dictionary<int, int>();
349349
status.Diff2RowID1 = new Dictionary<int, int>();
350350
status.Diff2RowID2 = new Dictionary<int, int>();
351+
status.DiffMaxLineCount = new Dictionary<int, int>();
351352
status.RowEdited1 = status.RowEdited1?? new Dictionary<int, Dictionary<int, CellEditMode>>();
352353
status.RowEdited2 = status.RowEdited2?? new Dictionary<int, Dictionary<int, CellEditMode>>();
353354

@@ -361,7 +362,8 @@ SheetDiffStatus DiffSheet(ISheet src, ISheet dst, SheetDiffStatus status = null)
361362
if (diffkv.Obj2.Key == null) {
362363
rowid2 = -1;
363364
}
364-
var diffrow = DiffSheetRow(src, rowid1, dst, rowid2, status);
365+
int maxLineCount = 0;
366+
var diffrow = DiffSheetRow(src, rowid1, dst, rowid2, status, out maxLineCount);
365367

366368
if (diffkv.Obj1.Key == null) {
367369
// 创建新行,方便比较,放在后面是为了保证diff的时候是new,delete的形式,而不是modify
@@ -376,6 +378,7 @@ SheetDiffStatus DiffSheet(ISheet src, ISheet dst, SheetDiffStatus status = null)
376378
status.column2diff2[rowid2] = getColumn2Diff(diffrow, false);
377379

378380
int diffIdx = status.diffSheet.Count;
381+
status.DiffMaxLineCount[diffIdx] = maxLineCount;
379382

380383
status.rowID2DiffMap1[rowid1] = diffIdx;
381384
status.rowID2DiffMap2[rowid2] = diffIdx;
@@ -777,23 +780,29 @@ List<DiffResult<string2int>> GetIDDiffList(ISheet sheet1, ISheet sheet2, int che
777780
return result.ToList();
778781
}
779782

780-
List<DiffResult<string>> DiffSheetRow(ISheet sheet1, int row1, ISheet sheet2, int row2, SheetDiffStatus status) {
783+
List<DiffResult<string>> DiffSheetRow(ISheet sheet1, int row1, ISheet sheet2, int row2, SheetDiffStatus status, out int maxLineCount) {
781784
var list1 = new List<string>();
782785
var list2 = new List<string>();
783786

787+
maxLineCount = 0;
784788
if (sheet1.GetRow(row1)!=null) {
785789
var row = sheet1.GetRow(row1);
786790
var columnCount = books["src"].SheetValideColumn[sheet1.SheetName];
787-
for (int i =0; i < columnCount; ++i) {
788-
list1.Add(Util.GetCellValue(row.GetCell(i)));
791+
for (int i =0; i < columnCount; ++i) {
792+
var value = Util.GetCellValue(row.GetCell(i));
793+
maxLineCount = Math.Max(maxLineCount, value.Count((c) => { return c == '\n'; }) + 1);
794+
795+
list1.Add(value);
789796
}
790797
}
791798

792799
if (sheet2.GetRow(row2) != null) {
793800
var row = sheet2.GetRow(row2);
794801
var columnCount = books["dst"].SheetValideColumn[sheet2.SheetName];
795802
for (int i = 0; i < columnCount; ++i) {
796-
list2.Add(Util.GetCellValue(row.GetCell(i)));
803+
var value = Util.GetCellValue(row.GetCell(i));
804+
maxLineCount = Math.Max(maxLineCount, value.Count((c) => { return c == '\n'; }) + 1);
805+
list2.Add(value);
797806
}
798807
}
799808
var diff = DiffUtil.Diff(list1, list2);

ExcelMerge/Util.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ public class SheetDiffStatus {
228228
public Dictionary<int, int> Diff2RowID1;
229229
public Dictionary<int, int> Diff2RowID2;
230230

231+
public Dictionary<int, int> DiffMaxLineCount;
232+
231233
public Dictionary<int, Dictionary<int, CellEditMode>> RowEdited1;
232234
public Dictionary<int, Dictionary<int, CellEditMode>> RowEdited2;
233235

@@ -291,6 +293,8 @@ public class ExcelData : DynamicObject {
291293
public int rowId;
292294
public string tag;
293295
public int diffIdx;
296+
public int maxLineCount=1;
297+
294298

295299
public SheetRowDiff diffstatus;
296300
public Dictionary<int, int> RowID2DiffMap;
@@ -301,7 +305,8 @@ public class ExcelData : DynamicObject {
301305
public override bool TryGetMember(GetMemberBinder binder, out object result) {
302306
CellData ret;
303307
if (data.TryGetValue(binder.Name, out ret)) {
304-
result = "<Bold> " + ret.value + "<Bold/>";
308+
//result = "<Bold> " + ret.value + "<Bold/>";
309+
result = ret.value;
305310
//var block = new TextBlock();
306311
//result = new Bold(new Run(ret.value));
307312
// result = block;

0 commit comments

Comments
 (0)