-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathForm1.cs
143 lines (133 loc) · 5.18 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
#region Copyright Syncfusion Inc. 2001-2021.
// Copyright Syncfusion Inc. 2001-2021. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DemoCommon.Grid;
using Syncfusion.XlsIO;
using Syncfusion.Windows.Forms;
using Syncfusion.Windows.Forms.Grid;
using Syncfusion.GridConverter;
using System.IO;
using Syncfusion.GridHelperClasses;
using Virtual_Import;
using Syncfusion.GroupingGridExcelConverter;
using Syncfusion.GridExcelConverter;
namespace Virtual_Import
{
public partial class Form1 : GridDemoForm
{
#region Private Variables
ExcelEngine excelEngine = new ExcelEngine();
IWorkbook workbook;
GridModel[] gridModelCollection;
ImportingHelper importingHelper;
#endregion
#region Constructor
public Form1()
{
InitializeComponent();
if (DpiAware.GetCurrentDpi() > 96)
{
this.CaptionBarHeight = (int)DpiAware.LogicalToDeviceUnits(this.CaptionBarHeight);
}
excelEngine = new ExcelEngine();
importingHelper = new ImportingHelper();
#if !NETCORE
FileStream fileStream = new FileStream(@"..\..\Data\Sample.xlsx", FileMode.Open);
#else
FileStream fileStream = new FileStream(@"..\..\..\Data\Sample.xlsx", FileMode.Open);
#endif
if (fileStream != null)
{
workbook = excelEngine.Excel.Workbooks.Open(fileStream, ExcelOpenType.Automatic);
importingHelper.LoadWorkbook(this.tabBarSplitterControl1, workbook);
gridModelCollection = importingHelper.gridModelCollection;
}
//Used to focus formula cell when it is edit mode
GridFormulaCellRenderer.ForceEditWhenActivated = false;
}
#endregion
#region Import Excel sheet
private void importButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Files(*.xls)|*.xls|Files(*.xlsx)|*xlsx";
openFileDialog.DefaultExt = ".xls";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
importingHelper.UnHookingEvents();
workbook.Close();
FileStream fileStream = new FileStream(openFileDialog.FileName, FileMode.Open);
if (fileStream != null)
{
workbook = excelEngine.Excel.Workbooks.Open(fileStream, ExcelOpenType.Automatic);
importingHelper.excelConverter.ImportBorders = this.importBordersCheckBox.Checked;
importingHelper.excelConverter.ImportStyles = this.importStylesCheckBox.Checked;
importingHelper.LoadWorkbook(this.tabBarSplitterControl1, workbook);
gridModelCollection = importingHelper.gridModelCollection;
}
}
}
#endregion
#region Saving Excel sheet
private void saveBtn_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xlsx";
sfd.FileName = "Untitled";
sfd.Filter = "Excel 97 to 2003 File(*.xls)|*.xls|Excel 2007 to 2013 File(*.xlsx)|*.xlsx";
sfd.FilterIndex = 2;
if (sfd.ShowDialog() == DialogResult.OK)
{
using (Stream stream = sfd.OpenFile())
{
if (sfd.FilterIndex == 1)
{
if (workbook.Version == ExcelVersion.Excel97to2003)
workbook.SaveAs(stream);
else
{
workbook.Version = ExcelVersion.Excel97to2003;
workbook.SaveAs(stream);
}
}
else if (sfd.FilterIndex == 2)
{
if (workbook.Version == ExcelVersion.Excel2007 || workbook.Version == ExcelVersion.Excel2010)
workbook.SaveAs(stream);
else
{
workbook.Version = ExcelVersion.Excel2013;
workbook.SaveAs(stream);
}
}
}
}
}
#endregion
#region Dispose
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
}