1-
2-
3- using FileFormat . Cells ;
4-
5- string outputDirectory = "/Users/fahadadeelqazi/Downloads/" ;
6-
7- string filePath = "/Users/fahadadeelqazi/Downloads/test_fahad_new_protected_image.xlsx" ;
8-
9-
10-
11- //using (Workbook wb = new Workbook(filePath))
12- //{
13- // wb.UpdateDefaultStyle("Arial", 12, "A02000");
14- // //Console.WriteLine("Style ID = " + styleId);
15- // wb.Save();
16- //}
17-
18-
19-
20- //using (Workbook wb = new Workbook(filePath)) // Open Existing workbook
21- //{
22-
23- // foreach (var worksheet in wb.Worksheets)
24- // {
25- // if (worksheet.IsProtected())
26- // {
27- // Console.WriteLine("Protect Sheet Name = " + worksheet.Name);
28- // worksheet.UnprotectSheet();
29- // }
30- // }
31- // // Save the workbook
32- // wb.Save(filePath);
33- //}
34-
35-
36-
37- //using (Workbook wb = new Workbook(filePath)) // Open Existing workbook
38- //{
39- // int i = 0;
40- // foreach (var worksheet in wb.Worksheets)
41- // {
42- // i++;
43- // worksheet.Name = $"Fahad{i}";
44- // Console.WriteLine(worksheet.Name);
45- // }
46- // // Save the workbook
47- // wb.Save(filePath);
48- //}
49-
50-
51-
52- //using (Workbook wb = new Workbook()) // Creating a new workbook
53- //{
54-
55- // Worksheet firstSheet = wb.Worksheets[0];
56- // firstSheet.SetRowHeight(1, 40); // Set height of row 1 to 40 points
57- // firstSheet.SetColumnWidth("B", 75);
58- // // Put values into cells
59- // Cell cellA1 = firstSheet.Cells["A1"];
60- // cellA1.PutValue("aaa A1");
61-
62- // // Repeat the process for other cells as needed
63- // Cell cellB2 = firstSheet.Cells["B2"];
64- // cellB2.PutValue("Styled Text");
65- // // Save the workbook
66- // wb.Save(filePath);
67- //}
68-
69-
70- using ( Workbook wb = new Workbook ( ) ) // Creating a new workbook
71- {
72- //Console.WriteLine("styleIndex = " + styleIndex);
73- Worksheet firstSheet = wb . Worksheets [ 0 ] ;
74- // Put values into cells
75- Cell cellA1 = firstSheet . Cells [ "A1" ] ;
76- cellA1 . PutValue ( "aaa A1" ) ;
77- firstSheet . ProtectSheet ( "a2387ass" ) ;
78- // Save the workbook
79- wb . Save ( filePath ) ;
80- }
81-
82-
83-
84- //Workbook wb = new Workbook(filePath);
85-
86- //var worksheet = wb.Worksheets[0];
87- //var images = worksheet.ExtractImages();
88- //if (!Directory.Exists(outputDirectory))
89- //{
90- // Directory.CreateDirectory(outputDirectory);
91- //}
92-
93- //foreach (var image in images)
94- //{
95- // // Construct file path
96- // var outputFilePath = Path.Combine(outputDirectory, $"Image_{Guid.NewGuid()}.{image.Extension}");
97-
98- // // Save image data to file
99- // using (var fileStream = File.Create(outputFilePath))
100- // {
101- // image.Data.CopyTo(fileStream);
102- // }
103- //}
104-
105-
106-
107-
108- //using (var workbook = new Workbook())
109- //{
110- // var firstSheet = workbook.Worksheets[0];
111- // var image = new Image("/Users/fahadadeelqazi/Downloads/ImageCells.png");
112- // Console.WriteLine("Image = " + image.Path);
113- // firstSheet.AddImage(image, 6, 1, 8, 3);
114-
115- // var secondSheet = workbook.AddSheet("fahad");
116-
117- // var image1 = new Image("/Users/fahadadeelqazi/Downloads/ImageCells.png");
118- // Console.WriteLine("Image = " + image.Path);
119- // secondSheet.AddImage(image1, 6, 1, 8, 3);
120-
121- // workbook.Save(filePath);
122- //}
123-
124-
125-
126- //using (Workbook wb = new Workbook()) // Creating a new workbook
127- //{
128- // // Create a style with Calibri font, size 11, and red color
129- // uint styleIndex = wb.CreateStyle("Arial", 11, "FF0000");
130- // uint styleIndex2 = wb.CreateStyle("Times New Roman", 12, "000000");
131-
132- // //Console.WriteLine("styleIndex = " + styleIndex);
133- // Worksheet firstSheet = wb.Worksheets[0];
134-
135- // // Put values into cells
136- // Cell cellA1 = firstSheet.Cells["A1"];
137- // cellA1.PutValue("aaa A1");
138-
139- // // Apply the style to the cell
140- // cellA1.ApplyStyle(styleIndex);
141-
142- // // Repeat the process for other cells as needed
143- // Cell cellB2 = firstSheet.Cells["B2"];
144- // cellB2.PutValue("Styled Text");
145- // cellB2.ApplyStyle(styleIndex2);
146-
147- // // Save the workbook
148- // wb.Save(filePath);
149- //}
150-
151- // Example code for Merge Cells
152-
153- //using (var workbook = new Workbook())
154- //{
155- // var firstSheet = workbook.Worksheets[0];
156- // firstSheet.MergeCells("A1", "C1"); // Merge cells from A1 to C1
157-
158- // // Add value to the top-left cell of the merged area
159- // var topLeftCell = firstSheet.Cells["A1"];
160- // topLeftCell.PutValue("This is a merged cell");
161-
162- // workbook.Save(filePath);
163- //}
164-
165-
166- // Example for setting Default Style for the whole workbook and some custom styles for cells.
167- //using (var workbook = new Workbook())
168- //{
169- // // Update default style and create new styles
170- // workbook.UpdateDefaultStyle("Times New Roman", 11, "000000");
171- // uint headerStyleIndex = workbook.CreateStyle("Arial", 15, "000000"); // Black for headers
172- // uint evenRowStyleIndex = workbook.CreateStyle("Arial", 12, "FF0000"); // Red for even
173- // uint oddRowStyleIndex = workbook.CreateStyle("Calibri", 12, "0000FF"); // Blue for odd
174-
175- // var firstSheet = workbook.Worksheets[0];
176-
177- // // Header row
178- // string[] headers = { "Student ID", "Student Name", "Course", "Grade" };
179- // for (int col = 0; col < headers.Length; col++)
180- // {
181- // string cellAddress = $"{(char)(65 + col)}1"; // A1, B1, etc.
182- // Cell cell = firstSheet.Cells[cellAddress];
183- // cell.PutValue(headers[col]);
184- // cell.ApplyStyle(headerStyleIndex);
185- // }
186-
187- // // Data rows
188- // int rowCount = 10;
189- // for (int row = 2; row <= rowCount + 1; row++) // Starting from row 2 because row 1 is header
190- // {
191- // for (int col = 0; col < headers.Length; col++)
192- // {
193- // string cellAddress = $"{(char)(65 + col)}{row}"; // Converts 0 to A, 1 to B, etc., and appends row number
194- // Cell cell = firstSheet.Cells[cellAddress];
195-
196- // // Sample data generation logic.
197- // switch (col)
198- // {
199- // case 0: // Student ID
200- // cell.PutValue($"ID{row - 1}");
201- // break;
202- // case 1: // Student Name
203- // cell.PutValue($"Student {row - 1}");
204- // break;
205- // case 2: // Course
206- // cell.PutValue($"Course {(row - 1) % 5 + 1}");
207- // break;
208- // case 3: // Grade
209- // cell.PutValue($"Grade {((row - 1) % 3) + 'A'}");
210- // break;
211- // }
212-
213- // // Apply different styles for even and odd rows
214- // cell.ApplyStyle((row % 2 == 0) ? evenRowStyleIndex : oddRowStyleIndex);
215- // }
216- // }
217-
218- // workbook.Save(filePath);
219- //}
220-
221-
222-
223-
224- // Properties example
225- //using (var workbook = new Workbook())
226- //{
227- // Worksheet firstSheet = workbook.Worksheets[0];
228- // Cell cellA1 = firstSheet.Cells["A1"];
229- // cellA1.PutValue("Text A1");
230-
231- // Cell cellA2 = firstSheet.Cells["A2"];
232- // cellA2.PutValue("Text A2");
233- // // Set new properties
234- // var newProperties = new BuiltInDocumentProperties
235- // {
236- // Author = "Fahad Adeel",
237- // Title = "Sample Workboo1",
238- // CreatedDate = DateTime.Now,
239- // ModifiedBy = "Fahad",
240- // ModifiedDate = DateTime.Now.AddHours(1),
241- // Subject = "Testing Subject"
242- // };
243-
244- // workbook.BuiltinDocumentProperties = newProperties;
245-
246- // workbook.Save(filePath);
247- //}
248-
249- //static void DisplayProperties(BuiltInDocumentProperties properties)
250- //{
251- // Console.WriteLine($"Author: {properties.Author}");
252- // Console.WriteLine($"Title: {properties.Title}");
253- // Console.WriteLine($"Created Date: {properties.CreatedDate}");
254- // Console.WriteLine($"Modified By: {properties.ModifiedBy}");
255- // Console.WriteLine($"Modified Date: {properties.ModifiedDate}");
256- // Console.WriteLine("=================================");
257- //}
258-
259-
260-
261-
262-
263-
264-
265-
266-
267- //Create a new workbook
268- //Scenario 1: Create a new workbook and save it to a specific file path.
269- //using (Workbook wb = new Workbook())
270- //{
271- // Worksheet firstSheet = wb.Worksheets[0];
272-
273- // // Put values into cells
274- // Cell cellA1 = firstSheet.Cells["A1"];
275- // cellA1.PutValue("aaa A1");
276-
277- // var newSheet = wb.AddSheet("FahadSheet");
278- // Cell cellB1 = newSheet.Cells["B1"];
279- // cellB1.PutValue("bbb B1");
280- // wb.Save(filePath);
281- //}
282-
283- //Example code for adding formula to cell.
284- //using (Workbook wb = new Workbook())
285- //{
286- // Worksheet firstSheet = wb.Worksheets[0];
287-
288- // Random rand = new Random();
289- // for (int i = 1; i <= 10; i++)
290- // {
291- // string cellReference = $"A{i}";
292- // Cell cell = firstSheet.Cells[cellReference];
293- // double randomValue = rand.Next(1, 100); // Generate random number between 1 and 100
294- // cell.PutValue(randomValue); // Put random number into cell
295- // }
296-
297- // Cell cellA11 = firstSheet.Cells["A11"];
298- // cellA11.PutFormula("SUM(A1:A10)"); // Putting a formula into cell A11 to sum A1 to A10
299- // wb.Save(filePath); // Saving the workbook
300-
301- // Console.WriteLine("VAAA=" +cellA11.GetValue());
302-
303- //}
304-
305- //using (Workbook wb = new Workbook(filePath))
306- //{
307- // Worksheet firstSheet = wb.Worksheets[0];
308-
309-
310- // Cell cellA11 = firstSheet.Cells["A11"];
311-
312-
313- // Console.WriteLine("VAAA11=" + cellA11.GetValue());
314-
315- //}
316-
317-
318-
319- // Output the value stored in a cell
320- //using (Workbook wb = new Workbook(filePath))
321- //{
322- // Worksheet firstSheet = wb.Worksheets[0];
323- // Cell cellA1 = firstSheet.Cells["A10"];
324- // Console.WriteLine(cellA1.GetDataType());
325- // string value = cellA1.GetValue();
326-
327- // Console.WriteLine(value); // Output the value stored in cell A1
328- //}
329-
330-
331- // Remove Worksheet by name
332-
333- //using (Workbook wb = new Workbook(filePath))
334- //{
335- // bool isRemoved = wb.RemoveSheet("FahadSheet");
336- // if (isRemoved)
337- // {
338- // // Save the workbook if the sheet is successfully removed
339- // wb.Save();
340- // }
341- //}
342-
343- //Scenario 2: Open an existing workbook, modify it, and save changes back to the original file.
344- //using (Workbook wb = new Workbook(filePath))
345- //{
346-
347- // Console.WriteLine(wb.Worksheets.Count);
348- // Worksheet firstSheet = wb.Worksheets[1];
349- // Cell cell = firstSheet.Cells["D2"];
350- // cell.PutValue("Fahad");
351- // wb.Save();
352- //}
353-
354- // Scenario 3: Open an existing workbook, modify it, and save to a MemoryStream.
355- //using (Workbook wb = new Workbook(filePath))
356- //{
357- // var newSheet = wb.AddNewSheet("NewSheetName2");
358- // Cell cell = newSheet["A1"];
359- // cell.PutValue("Hello from another new sheet!");
360- // using (MemoryStream ms = new MemoryStream())
361- // {
362- // wb.Save(ms);
363-
364- // // Do something with the MemoryStream, such as sending it to a client, etc.
365- // }
366- //}
1+
0 commit comments