Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4283edb

Browse files
upgrade to .NET 8
1 parent 348aeca commit 4283edb

22 files changed

+1803
-98
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using DevExpress.Spreadsheet;
2+
using DevExpress.XtraCharts;
3+
using System;
4+
using System.Xml;
5+
6+
namespace SpreadsheetDocServerAPIPart2
7+
{
8+
class CustomXmlPartActions
9+
{
10+
public static Action<Workbook> StoreCustomXmlPartAction = StoreCustomXmlPart;
11+
public static Action<Workbook> ObtainCustomXmlPartAction = ObtainCustomXmlPart;
12+
public static Action<Workbook> ModifyCustomXmlPartAction = ModifyCustomXmlPart;
13+
14+
15+
static void StoreCustomXmlPart(Workbook workbook)
16+
{
17+
#region #StoreCustomXmlPart
18+
workbook.Worksheets[0].Cells["A1"].Value = "Custom Xml Test";
19+
20+
// Add an empty custom XML part.
21+
ICustomXmlPart part = workbook.CustomXmlParts.Add();
22+
XmlElement elem = part.CustomXmlPartDocument.CreateElement("Person");
23+
elem.InnerText = "Stephen Edwards";
24+
part.CustomXmlPartDocument.AppendChild(elem);
25+
26+
// Add an XML part created from string.
27+
string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
28+
<whitepaper>
29+
<contact>
30+
<firstname>Roger</firstname>
31+
<lastname>Edwards</lastname>
32+
<phone>832-433-0025</phone>
33+
<address>1657 Wines Lane Houston, TX 77099</address>
34+
</contact>
35+
<date>2016年05月18日</date>
36+
</whitepaper>";
37+
workbook.CustomXmlParts.Add(xmlString);
38+
39+
// Add an XML part loaded from a file.
40+
XmlDocument xmlDoc = new XmlDocument();
41+
xmlDoc.Load("..\\..\\..\\Documents\\fishes.xml");
42+
workbook.CustomXmlParts.Add(xmlDoc);
43+
workbook.SaveDocument("..\\..\\..\\Documents\\CustomXmlTest.xlsx");
44+
System.IO.File.Copy("..\\..\\..\\Documents\\CustomXmlTest.xlsx", "..\\..\\..\\Documents\\CustomXmlTest.xlsx.zip", true);
45+
System.Diagnostics.Process.Start("..\\..\\..\\Documents\\CustomXmlTest.xlsx.zip");
46+
#endregion #StoreCustomXmlPart
47+
}
48+
49+
static void ObtainCustomXmlPart(IWorkbook workbook)
50+
{
51+
#region #ObtainCustomXmlPart
52+
// Load a document from a file.
53+
workbook.LoadDocument("..\\..\\..\\Documents\\CustomXml.xlsx");
54+
55+
// Access a custom XML file stored in the document.
56+
XmlDocument xmlDoc = workbook.CustomXmlParts[0].CustomXmlPartDocument;
57+
58+
// Retrieve a hyperlink from the XML file and display it in the document.
59+
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
60+
string xPathString = "//Fish[Category='Cod']/ScientificClassification/Reference";
61+
XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode(xPathString, nsmgr);
62+
string hLink = xmlNode.InnerText;
63+
workbook.Worksheets[0].Hyperlinks.Add(workbook.Worksheets[0].Cells["A2"], hLink, true);
64+
#endregion #ObtainCustomXmlPart
65+
}
66+
67+
static void ModifyCustomXmlPart(IWorkbook workbook)
68+
{
69+
#region #ModifyCustomXmlPart
70+
// Load a document from a file.
71+
workbook.LoadDocument("..\\..\\..\\Documents\\CustomXml.xlsx");
72+
73+
// Access a custom XML file stored in the document.
74+
XmlDocument xmlDoc = workbook.CustomXmlParts[1].CustomXmlPartDocument;
75+
76+
// Retrieve XML nodes that match a specific expression.
77+
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
78+
string xPathString = "//whitepaper/contact[firstname='Roger']/firstname";
79+
XmlNodeList xmlNodes = xmlDoc.DocumentElement.SelectNodes(xPathString, nsmgr);
80+
81+
// Modify XML nodes and display them in the document.
82+
foreach (XmlNode node in xmlNodes) node.InnerText = "Stephen";
83+
workbook.SaveDocument("..\\..\\..\\Documents\\CustomXmlRogerStephen.xlsx");
84+
workbook.Worksheets[0].Cells["A2"].Value = xmlDoc.FirstChild.FirstChild.FirstChild.InnerText;
85+
#endregion #ModifyCustomXmlPart
86+
}
87+
}
88+
}
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
using System;
2+
using DevExpress.Spreadsheet;
3+
using System.Drawing;
4+
using System.Linq;
5+
using DevExpress.XtraCharts;
6+
using DevExpress.XtraExport.Helpers;
7+
8+
namespace SpreadsheetDocServerAPIPart2
9+
{
10+
public static class DataValidationActions {
11+
12+
public static Action<Workbook> AddDataValidationAction = AddDataValidation;
13+
public static Action<Workbook> ChangeCriteriaAction = ChangeCriteria;
14+
public static Action<Workbook> UseUnionRangeAction = UseUnionRange;
15+
public static Action<Workbook> ShowInputMessageAction = ShowInputMessage;
16+
public static Action<Workbook> ShowErrorMessageAction = ShowErrorMessage;
17+
public static Action<Workbook> GetDataValidationAction = GetDataValidation;
18+
public static Action<Workbook> ValidateCellValueAction = ValidateCellValue;
19+
public static Action<Workbook> RemoveDataValidationAction = RemoveDataValidation;
20+
public static Action<Workbook> RemoveAllDataValidationsAction = RemoveAllDataValidations;
21+
22+
static void AddDataValidation(IWorkbook workbook) {
23+
#region #AddDataValidation
24+
// Load a document from a file.
25+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
26+
27+
// Access a worksheet.
28+
Worksheet worksheet = workbook.Worksheets[0];
29+
30+
// Specify the "C1" cell value.
31+
worksheet["C1"].SetValue(DateTime.Now);
32+
worksheet["C1"].NumberFormat = "mmm/d/yyyy h:mm";
33+
34+
// Restrict data entry to a whole number from 10 to 20.
35+
worksheet.DataValidations.Add(worksheet["B1"],
36+
DataValidationType.WholeNumber, DataValidationOperator.Between, 10, 20);
37+
38+
// Restrict data entry to a number within limits.
39+
DataValidation validation = worksheet.DataValidations.Add(worksheet["F4:F11"], DataValidationType.Decimal, DataValidationOperator.Between, 10, 40);
40+
41+
// Restrict data entry based on a formula.
42+
worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");
43+
44+
// Restrict data entry to 3 symbols.
45+
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
46+
47+
// Restrict data entry to values in a drop-down list specified in code.
48+
// Note that the list in code uses comma to separate entries,
49+
// but the list in UI displays a culture-specific list separator.
50+
worksheet.DataValidations.Add(worksheet["A4:A11"], DataValidationType.List, "PASS, FAIL");
51+
52+
// Restrict data entry to values in a drop-down list obtained from a worksheet.
53+
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
54+
55+
// Restrict data entry to a time before the specified time.
56+
worksheet.DataValidations.Add(worksheet["C1"], DataValidationType.Time, DataValidationOperator.LessThanOrEqual, DateTime.Now);
57+
58+
// Highlight data validation ranges.
59+
worksheet["H4:H9"].FillColor = Color.LightGray;
60+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3, 0xFFDFC4, 0xFFDAE9};
61+
for (int i = 0; i < worksheet.DataValidations.Count; i++){
62+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
63+
}
64+
#endregion #AddDataValidation
65+
}
66+
67+
static void ChangeCriteria(IWorkbook workbook) {
68+
#region #ChangeCriteria
69+
// Load a document from a file.
70+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
71+
72+
// Access a worksheet.
73+
Worksheet worksheet = workbook.Worksheets[0];
74+
75+
// Restrict data entry to a number within limits.
76+
DataValidation validation = worksheet.DataValidations.Add
77+
(worksheet["F4:F11"], DataValidationType.Decimal, DataValidationOperator.Between, 10, 40);
78+
79+
// Change the validation operator and criteria.
80+
// The "F4:F11" range's cell values are valid
81+
// if they are greater than or equal 20.
82+
validation.Operator = DataValidationOperator.GreaterThanOrEqual;
83+
validation.Criteria = 20;
84+
validation.Criteria2 = ValueObject.Empty;
85+
86+
// Highlight data validation ranges.
87+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
88+
for (int i = 0; i < worksheet.DataValidations.Count; i++)
89+
{
90+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
91+
}
92+
#endregion #ChangeCriteria
93+
}
94+
95+
static void UseUnionRange(IWorkbook workbook) {
96+
#region #UseUnionRange
97+
// Load a document from a file.
98+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
99+
100+
// Access a worksheet.
101+
Worksheet worksheet = workbook.Worksheets[0];
102+
103+
// Create a union range.
104+
CellRange range = worksheet.Range.Union(worksheet["F4:F5"], worksheet["F6:F11"]);
105+
106+
// Restrict data entry to a number within limits.
107+
worksheet.DataValidations.Add(range, DataValidationType.Decimal, DataValidationOperator.Between, 10, 40);
108+
109+
// Highlight data validation ranges.
110+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
111+
for (int i = 0; i < worksheet.DataValidations.Count; i++)
112+
{
113+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
114+
}
115+
#endregion #UseUnionRange
116+
}
117+
118+
static void ShowInputMessage(IWorkbook workbook) {
119+
#region #ShowInputMessage
120+
// Load a document from a file.
121+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
122+
123+
// Access a worksheet.
124+
Worksheet worksheet = workbook.Worksheets[0];
125+
126+
// Restrict data entry to a 5-digit number.
127+
DataValidation validation = worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");
128+
129+
// Show input message.
130+
validation.InputTitle = "Employee Id";
131+
validation.InputMessage = "Please enter 5-digit number";
132+
validation.ShowInputMessage = true;
133+
134+
// Highlight data validation ranges.
135+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
136+
for (int i = 0; i < worksheet.DataValidations.Count; i++)
137+
{
138+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
139+
}
140+
#endregion #ShowInputMessage
141+
}
142+
143+
static void ShowErrorMessage(IWorkbook workbook) {
144+
#region #ShowErrorMessage
145+
// Load a document from a file.
146+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
147+
148+
// Access a worksheet.
149+
Worksheet worksheet = workbook.Worksheets[0];
150+
151+
// Restrict data entry to a 5-digit number.
152+
DataValidation validation = worksheet.DataValidations.Add(worksheet["B4:B11"], DataValidationType.Custom, "=AND(ISNUMBER(B4),LEN(B4)=5)");
153+
154+
// Show error message.
155+
validation.ErrorTitle = "Wrong Employee Id";
156+
validation.ErrorMessage = "The value you entered is not valid. Use 5-digit number for the employee ID.";
157+
validation.ErrorStyle = DataValidationErrorStyle.Information;
158+
validation.ShowErrorMessage = true;
159+
160+
// Highlight data validation ranges.
161+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
162+
for (int i = 0; i < worksheet.DataValidations.Count; i++)
163+
{
164+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
165+
}
166+
#endregion #ShowErrorMessage
167+
}
168+
169+
static void GetDataValidation(IWorkbook workbook)
170+
{
171+
#region #GetDataValidation
172+
// Load a document from a file.
173+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
174+
175+
// Access a worksheet.
176+
Worksheet worksheet = workbook.Worksheets[0];
177+
178+
// Add data validations.
179+
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
180+
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
181+
182+
// Get data validation entry associated with the "E4" cell.
183+
worksheet.DataValidations.GetDataValidation(worksheet.Cells["E4"]).Criteria = ValueObject.FromRange(worksheet["H4:H5"]);
184+
185+
// Get data validation entries for the "D4:E11" range.
186+
var myValidation = worksheet.DataValidations.GetDataValidations(worksheet["D4:E11"])
187+
.Where(d => d.ValidationType == DataValidationType.TextLength).SingleOrDefault();
188+
if (myValidation != null) myValidation.Criteria = 4;
189+
190+
// Get data validation entries that meet a specific criteria.
191+
foreach (var d in worksheet.DataValidations.GetDataValidations(DataValidationType.TextLength, DataValidationOperator.Equal, 4, ValueObject.Empty))
192+
{
193+
// Change the validation operator.
194+
// The "D4:D11" range's cell values are valid
195+
// if they contain text with more than 4 characters.
196+
d.Operator = DataValidationOperator.GreaterThan;
197+
}
198+
199+
// Highlight data validation ranges.
200+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
201+
for (int i = 0; i < worksheet.DataValidations.Count; i++)
202+
{
203+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
204+
}
205+
#endregion #GetDataValidation
206+
}
207+
208+
static void ValidateCellValue(IWorkbook workbook)
209+
{
210+
#region #ValidateCellValue
211+
// Load a document from a file.
212+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
213+
214+
// Access a worksheet.
215+
Worksheet worksheet = workbook.Worksheets[0];
216+
217+
// Add data validations.
218+
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
219+
220+
// Check whether the cell value meets a validation criteria.
221+
bool isValid = worksheet.DataValidations.Validate(worksheet.Cells["D4"], worksheet.Cells["J4"].Value);
222+
if (isValid) { worksheet["D4"].CopyFrom(worksheet["J4"]); }
223+
#endregion #ValidateCellValue
224+
}
225+
226+
static void RemoveDataValidation(IWorkbook workbook) {
227+
#region #RemoveDataValidation
228+
// Load a document from a file.
229+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
230+
231+
// Access a worksheet.
232+
Worksheet worksheet = workbook.Worksheets[0];
233+
234+
// Add data validations.
235+
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
236+
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
237+
238+
// Remove a data validation by its index.
239+
worksheet.DataValidations.RemoveAt(1);
240+
241+
// Highlight data validation ranges.
242+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
243+
for (int i = 0; i < worksheet.DataValidations.Count; i++)
244+
{
245+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
246+
}
247+
#endregion #RemoveDataValidation
248+
}
249+
250+
static void RemoveAllDataValidations(IWorkbook workbook) {
251+
#region #RemoveAllDataValidations
252+
// Load a document from a file.
253+
workbook.LoadDocument("..\\..\\..\\Documents\\DataValidation.xlsx");
254+
255+
// Access a worksheet.
256+
Worksheet worksheet = workbook.Worksheets[0];
257+
258+
// Add data validations.
259+
worksheet.DataValidations.Add(worksheet["D4:D11"], DataValidationType.TextLength, DataValidationOperator.Equal, 3);
260+
worksheet.DataValidations.Add(worksheet["E4:E11"], DataValidationType.List, ValueObject.FromRange(worksheet["H4:H9"].GetRangeWithAbsoluteReference()));
261+
262+
// Remove all data validations.
263+
worksheet.DataValidations.Clear();
264+
265+
// Highlight data validation ranges.
266+
int[] MyColorScheme = new int[] { 0xFFC4C4, 0xFFD9D9, 0xFFF6F6, 0xFFECEC, 0xE9D3D3 };
267+
for (int i = 0; i < worksheet.DataValidations.Count; i++)
268+
{
269+
worksheet.DataValidations[i].Range.FillColor = Color.FromArgb(MyColorScheme[i]);
270+
}
271+
#endregion #RemoveAllDataValidations
272+
}
273+
}
274+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /