pub struct WorkSheet { /* private fields */ }Implementations§
Source§impl WorkSheet
impl WorkSheet
Sourcepub fn autofilter<L: LocationRange>(&mut self, loc_range: L)
pub fn autofilter<L: LocationRange>(&mut self, loc_range: L)
Examples found in repository?
examples/autofilter.rs (line 38)
4fn main() -> WorkbookResult<()> {
5 // Prepare autofilter data
6 let text = fs::read_to_string("examples/autofilter_data.txt").unwrap();
7 let mut text = text.split("\n");
8 let headers: Vec<&str> = text.next().unwrap().split_whitespace().collect();
9 let mut data: Vec<Vec<&str>> = vec![];
10 for text in text { data.push(text.split_whitespace().collect()) }
11
12 // Create a new workbook
13 let mut workbook = Workbook::new();
14 // Add some worksheets
15 workbook.add_worksheet()?;
16 workbook.add_worksheet()?;
17 workbook.add_worksheet()?;
18 workbook.add_worksheet()?;
19 workbook.add_worksheet()?;
20 workbook.add_worksheet()?;
21
22 // Set up several sheets with the same data.
23 for worksheet in workbook.worksheets_mut() {
24 // Make the columns wider.
25 worksheet.set_columns_width("A:D", 12.0)?;
26 // // Make the header row larger.
27 worksheet.set_row_height_with_format(1, 20.0, &Format::default().set_bold())?;
28 // Make the headers bold.
29 worksheet.write_row("A1", &headers)?;
30 }
31
32 //
33 // Example 1. Autofilter without conditions.
34 //
35
36 let worksheet1 = workbook.get_worksheet_mut(1)?;
37 // Set the autofilter.
38 worksheet1.autofilter("A1:D51");
39 let mut row = 2;
40 for row_data in &data {
41 let mut col = 1;
42 for data in row_data {
43 if let Ok(num) = data.parse::<i32>() {
44 worksheet1.write((row, col), num)?;
45 } else {
46 worksheet1.write((row, col), *data)?;
47 }
48 col += 1;
49 }
50 // Move on to the next worksheet row.
51 row += 1;
52 }
53
54
55 //
56 // Example 2. Autofilter with a filter condition in the first column.
57 //
58 let worksheet2 = workbook.get_worksheet_mut(2)?;
59 // Set the autofilter.
60 worksheet2.autofilter("A1:D51");
61 // Add filter criteria.
62 let mut filters = Filters::new();
63 filters.and(Filter::eq("East"));
64 worksheet2.filter_column("A", &filters);
65 // Hide the rows that don't match the filter criteria.
66 let mut row = 2;
67 for row_data in &data {
68 let mut col = 1;
69 let data = row_data.get(0);
70 // Check for rows that match the filter.
71 if data != Some(&"East") {
72 // We need to hide rows that don't match the filter.
73 worksheet2.hide_row(row)?;
74 }
75 for data in row_data {
76 if let Ok(num) = data.parse::<i32>() {
77 worksheet2.write((row, col), num)?;
78 } else {
79 worksheet2.write((row, col), *data)?;
80 }
81 col += 1;
82 }
83 // Move on to the next worksheet row.
84 row += 1;
85 }
86
87 //
88 // Example 3. Autofilter with a filter condition in the first column.
89 //
90 let worksheet3 = workbook.get_worksheet_mut(3)?;
91 // Set the autofilter.
92 worksheet3.autofilter("A1:D51");
93 // Add filter criteria.
94 let mut filters = Filters::new();
95 filters.and(Filter::eq("East")).or(Filter::eq("South"));
96 worksheet3.filter_column("A", &filters);
97 // Hide the rows that don't match the filter criteria.
98 let mut row = 2;
99 for row_data in &data {
100 let mut col = 1;
101 let data = row_data.get(0);
102 // Check for rows that match the filter.
103 if data != Some(&"East") && data != Some(&"South") {
104 // We need to hide rows that don't match the filter.
105 worksheet3.hide_row(row)?;
106 }
107 for data in row_data {
108 if let Ok(num) = data.parse::<i32>() {
109 worksheet3.write((row, col), num)?;
110 } else {
111 worksheet3.write((row, col), *data)?;
112 }
113 col += 1;
114 }
115 // Move on to the next worksheet row.
116 row += 1;
117 }
118
119
120 //
121 // Example 4. Autofilter with filter conditions in two columns.
122 //
123 let worksheet4 = workbook.get_worksheet_mut(4)?;
124 // Set the autofilter.
125 worksheet4.autofilter("A1:D51");
126 // Add filter criteria.
127 let mut filters_a = Filters::new();
128 filters_a.and(Filter::eq("East"));
129 worksheet4.filter_column("A", &filters_a);
130 let mut filters_c = Filters::new();
131 filters_c.and(Filter::gt("3000")).and(Filter::lt("8000"));
132 worksheet4.filter_column("C", &filters_c);
133 // Hide the rows that don't match the filter criteria.
134 let mut row = 2;
135 for row_data in &data {
136 let mut col = 1;
137 let data = row_data.get(0);
138 // Check for rows that match the filter.
139 if data != Some(&"East") {
140 // We need to hide rows that don't match the filter.
141 worksheet4.hide_row(row)?;
142 }
143 for data in row_data {
144 if let Ok(num) = data.parse::<i32>() {
145 if num <= 3000 || num >= 8000 {
146 worksheet4.hide_row(row)?;
147 }
148 worksheet4.write((row, col), num)?;
149 } else {
150 worksheet4.write((row, col), *data)?;
151 }
152 col += 1;
153 }
154 // Move on to the next worksheet row.
155 row += 1;
156 }
157
158
159 //
160 // Example 5. Autofilter with a filter list condition in one of the columns.
161 //
162 let worksheet5 = workbook.get_worksheet_mut(5)?;
163 // Set the autofilter.
164 worksheet5.autofilter("A1:D51");
165 // Add filter criteria.
166 let filters_list = Filters::eq(vec!["East", "North", "South"]);
167 worksheet5.filter_column("A", &filters_list);
168 // Hide the rows that don't match the filter criteria.
169 let mut row = 2;
170 for row_data in &data {
171 let mut col = 1;
172 let data = row_data.get(0);
173 // Check for rows that match the filter.
174 if data != Some(&"East") && data != Some(&"North") && data != Some(&"South") {
175 // We need to hide rows that don't match the filter.
176 worksheet5.hide_row(row)?;
177 }
178 for data in row_data {
179 if let Ok(num) = data.parse::<i32>() {
180 worksheet5.write((row, col), num)?;
181 } else {
182 worksheet5.write((row, col), *data)?;
183 }
184 col += 1;
185 }
186 // Move on to the next worksheet row.
187 row += 1;
188 }
189
190 //
191 // Example 6. Autofilter with filter for blanks.
192 //
193 let worksheet6 = workbook.get_worksheet_mut(6)?;
194 // Set the autofilter.
195 worksheet6.autofilter("A1:D51");
196 // Add filter criteria.
197 let filters = Filters::blank();
198 worksheet6.filter_column("A", &filters);
199 // Hide the rows that don't match the filter criteria.
200 let mut row = 2;
201 // Simulate a blank cell in the data.
202 data[5][0] = "";
203
204 for row_data in &data {
205 let mut col = 1;
206 let data = row_data.get(0);
207 // Check for rows that match the filter.
208 if data != Some(&"") {
209 // We need to hide rows that don't match the filter.
210 worksheet6.hide_row(row)?;
211 }
212 for data in row_data {
213 if let Ok(num) = data.parse::<i32>() {
214 worksheet6.write((row, col), num)?;
215 } else {
216 worksheet6.write((row, col), *data)?;
217 }
218 col += 1;
219 }
220 // Move on to the next worksheet row.
221 row += 1;
222 }
223
224 //
225 // Example 7. Autofilter with filter for non-blanks.
226 //
227 let worksheet7 = workbook.get_worksheet_mut(7)?;
228 // Set the autofilter.
229 worksheet7.autofilter("A1:D51");
230 // Add filter criteria.
231 let filters = Filters::not_blank();
232 worksheet7.filter_column("A", &filters);
233 // Hide the rows that don't match the filter criteria.
234 let mut row = 2;
235 // Simulate a blank cell in the data.
236
237 for row_data in &data {
238 let mut col = 1;
239 let data = row_data.get(0);
240 // Check for rows that match the filter.
241 if data == Some(&"") {
242 // We need to hide rows that don't match the filter.
243 worksheet7.hide_row(row)?;
244 }
245 for data in row_data {
246 if let Ok(num) = data.parse::<i32>() {
247 worksheet7.write((row, col), num)?;
248 } else {
249 worksheet7.write((row, col), *data)?;
250 }
251 col += 1;
252 }
253 // Move on to the next worksheet row.
254 row += 1;
255 }
256
257
258 workbook.save_as("examples/autofilter.xlsx")?;
259
260 Ok(())
261}Sourcepub fn filter_column<L: Location>(&mut self, col: L, filters: &Filters<'_>)
pub fn filter_column<L: Location>(&mut self, col: L, filters: &Filters<'_>)
Examples found in repository?
examples/autofilter.rs (line 64)
4fn main() -> WorkbookResult<()> {
5 // Prepare autofilter data
6 let text = fs::read_to_string("examples/autofilter_data.txt").unwrap();
7 let mut text = text.split("\n");
8 let headers: Vec<&str> = text.next().unwrap().split_whitespace().collect();
9 let mut data: Vec<Vec<&str>> = vec![];
10 for text in text { data.push(text.split_whitespace().collect()) }
11
12 // Create a new workbook
13 let mut workbook = Workbook::new();
14 // Add some worksheets
15 workbook.add_worksheet()?;
16 workbook.add_worksheet()?;
17 workbook.add_worksheet()?;
18 workbook.add_worksheet()?;
19 workbook.add_worksheet()?;
20 workbook.add_worksheet()?;
21
22 // Set up several sheets with the same data.
23 for worksheet in workbook.worksheets_mut() {
24 // Make the columns wider.
25 worksheet.set_columns_width("A:D", 12.0)?;
26 // // Make the header row larger.
27 worksheet.set_row_height_with_format(1, 20.0, &Format::default().set_bold())?;
28 // Make the headers bold.
29 worksheet.write_row("A1", &headers)?;
30 }
31
32 //
33 // Example 1. Autofilter without conditions.
34 //
35
36 let worksheet1 = workbook.get_worksheet_mut(1)?;
37 // Set the autofilter.
38 worksheet1.autofilter("A1:D51");
39 let mut row = 2;
40 for row_data in &data {
41 let mut col = 1;
42 for data in row_data {
43 if let Ok(num) = data.parse::<i32>() {
44 worksheet1.write((row, col), num)?;
45 } else {
46 worksheet1.write((row, col), *data)?;
47 }
48 col += 1;
49 }
50 // Move on to the next worksheet row.
51 row += 1;
52 }
53
54
55 //
56 // Example 2. Autofilter with a filter condition in the first column.
57 //
58 let worksheet2 = workbook.get_worksheet_mut(2)?;
59 // Set the autofilter.
60 worksheet2.autofilter("A1:D51");
61 // Add filter criteria.
62 let mut filters = Filters::new();
63 filters.and(Filter::eq("East"));
64 worksheet2.filter_column("A", &filters);
65 // Hide the rows that don't match the filter criteria.
66 let mut row = 2;
67 for row_data in &data {
68 let mut col = 1;
69 let data = row_data.get(0);
70 // Check for rows that match the filter.
71 if data != Some(&"East") {
72 // We need to hide rows that don't match the filter.
73 worksheet2.hide_row(row)?;
74 }
75 for data in row_data {
76 if let Ok(num) = data.parse::<i32>() {
77 worksheet2.write((row, col), num)?;
78 } else {
79 worksheet2.write((row, col), *data)?;
80 }
81 col += 1;
82 }
83 // Move on to the next worksheet row.
84 row += 1;
85 }
86
87 //
88 // Example 3. Autofilter with a filter condition in the first column.
89 //
90 let worksheet3 = workbook.get_worksheet_mut(3)?;
91 // Set the autofilter.
92 worksheet3.autofilter("A1:D51");
93 // Add filter criteria.
94 let mut filters = Filters::new();
95 filters.and(Filter::eq("East")).or(Filter::eq("South"));
96 worksheet3.filter_column("A", &filters);
97 // Hide the rows that don't match the filter criteria.
98 let mut row = 2;
99 for row_data in &data {
100 let mut col = 1;
101 let data = row_data.get(0);
102 // Check for rows that match the filter.
103 if data != Some(&"East") && data != Some(&"South") {
104 // We need to hide rows that don't match the filter.
105 worksheet3.hide_row(row)?;
106 }
107 for data in row_data {
108 if let Ok(num) = data.parse::<i32>() {
109 worksheet3.write((row, col), num)?;
110 } else {
111 worksheet3.write((row, col), *data)?;
112 }
113 col += 1;
114 }
115 // Move on to the next worksheet row.
116 row += 1;
117 }
118
119
120 //
121 // Example 4. Autofilter with filter conditions in two columns.
122 //
123 let worksheet4 = workbook.get_worksheet_mut(4)?;
124 // Set the autofilter.
125 worksheet4.autofilter("A1:D51");
126 // Add filter criteria.
127 let mut filters_a = Filters::new();
128 filters_a.and(Filter::eq("East"));
129 worksheet4.filter_column("A", &filters_a);
130 let mut filters_c = Filters::new();
131 filters_c.and(Filter::gt("3000")).and(Filter::lt("8000"));
132 worksheet4.filter_column("C", &filters_c);
133 // Hide the rows that don't match the filter criteria.
134 let mut row = 2;
135 for row_data in &data {
136 let mut col = 1;
137 let data = row_data.get(0);
138 // Check for rows that match the filter.
139 if data != Some(&"East") {
140 // We need to hide rows that don't match the filter.
141 worksheet4.hide_row(row)?;
142 }
143 for data in row_data {
144 if let Ok(num) = data.parse::<i32>() {
145 if num <= 3000 || num >= 8000 {
146 worksheet4.hide_row(row)?;
147 }
148 worksheet4.write((row, col), num)?;
149 } else {
150 worksheet4.write((row, col), *data)?;
151 }
152 col += 1;
153 }
154 // Move on to the next worksheet row.
155 row += 1;
156 }
157
158
159 //
160 // Example 5. Autofilter with a filter list condition in one of the columns.
161 //
162 let worksheet5 = workbook.get_worksheet_mut(5)?;
163 // Set the autofilter.
164 worksheet5.autofilter("A1:D51");
165 // Add filter criteria.
166 let filters_list = Filters::eq(vec!["East", "North", "South"]);
167 worksheet5.filter_column("A", &filters_list);
168 // Hide the rows that don't match the filter criteria.
169 let mut row = 2;
170 for row_data in &data {
171 let mut col = 1;
172 let data = row_data.get(0);
173 // Check for rows that match the filter.
174 if data != Some(&"East") && data != Some(&"North") && data != Some(&"South") {
175 // We need to hide rows that don't match the filter.
176 worksheet5.hide_row(row)?;
177 }
178 for data in row_data {
179 if let Ok(num) = data.parse::<i32>() {
180 worksheet5.write((row, col), num)?;
181 } else {
182 worksheet5.write((row, col), *data)?;
183 }
184 col += 1;
185 }
186 // Move on to the next worksheet row.
187 row += 1;
188 }
189
190 //
191 // Example 6. Autofilter with filter for blanks.
192 //
193 let worksheet6 = workbook.get_worksheet_mut(6)?;
194 // Set the autofilter.
195 worksheet6.autofilter("A1:D51");
196 // Add filter criteria.
197 let filters = Filters::blank();
198 worksheet6.filter_column("A", &filters);
199 // Hide the rows that don't match the filter criteria.
200 let mut row = 2;
201 // Simulate a blank cell in the data.
202 data[5][0] = "";
203
204 for row_data in &data {
205 let mut col = 1;
206 let data = row_data.get(0);
207 // Check for rows that match the filter.
208 if data != Some(&"") {
209 // We need to hide rows that don't match the filter.
210 worksheet6.hide_row(row)?;
211 }
212 for data in row_data {
213 if let Ok(num) = data.parse::<i32>() {
214 worksheet6.write((row, col), num)?;
215 } else {
216 worksheet6.write((row, col), *data)?;
217 }
218 col += 1;
219 }
220 // Move on to the next worksheet row.
221 row += 1;
222 }
223
224 //
225 // Example 7. Autofilter with filter for non-blanks.
226 //
227 let worksheet7 = workbook.get_worksheet_mut(7)?;
228 // Set the autofilter.
229 worksheet7.autofilter("A1:D51");
230 // Add filter criteria.
231 let filters = Filters::not_blank();
232 worksheet7.filter_column("A", &filters);
233 // Hide the rows that don't match the filter criteria.
234 let mut row = 2;
235 // Simulate a blank cell in the data.
236
237 for row_data in &data {
238 let mut col = 1;
239 let data = row_data.get(0);
240 // Check for rows that match the filter.
241 if data == Some(&"") {
242 // We need to hide rows that don't match the filter.
243 worksheet7.hide_row(row)?;
244 }
245 for data in row_data {
246 if let Ok(num) = data.parse::<i32>() {
247 worksheet7.write((row, col), num)?;
248 } else {
249 worksheet7.write((row, col), *data)?;
250 }
251 col += 1;
252 }
253 // Move on to the next worksheet row.
254 row += 1;
255 }
256
257
258 workbook.save_as("examples/autofilter.xlsx")?;
259
260 Ok(())
261}Source§impl WorkSheet
impl WorkSheet
Sourcepub fn max_column(&self) -> u32
pub fn max_column(&self) -> u32
Examples found in repository?
examples/xlsx2adoc.rs (line 71)
67fn find_col_width(sheet: &WorkSheet) -> Result<Vec<f64>, Error> {
68 let mut widths = Vec::<f64>::new();
69 let default_col_width = sheet.get_default_column().unwrap_or(1.0);
70
71 for _ in 0..sheet.max_column() {
72 widths.push(default_col_width);
73 }
74
75 let formatted_col_result = sheet.get_columns_with_format((1, 1, 1, 16384));
76 let formatted_col = match formatted_col_result {
77 Ok(f) => f,
78 Err(e) => return Err(error_text(&format!("{:?}", e))),
79 };
80
81 for w in formatted_col.iter() {
82 let column_name = w.0;
83 let a = w.1;
84 let columns_specs = a.0;
85 let column_width = columns_specs.width;
86 match column_width {
87 Some(width) => {
88 let col_range = decode_col_range(column_name, widths.len());
89 for c in col_range {
90 widths[c] = width;
91 }
92 }
93 None => {},
94 };
95}
96 Ok(widths)
97}
98
99pub fn xlsx_convert(
100 in_file_name: &Path,
101 out_file_name: &Path,
102) -> Result<Xlsx2AdocTestResults, Error> {
103 let workbook = Workbook::from_path(in_file_name);
104 let mut workbook = workbook.unwrap();
105 workbook.finish();
106
107 let reading_sheet = workbook.get_worksheet(1);
108 let sheet = reading_sheet.unwrap();
109 let default_row_hight = sheet.get_default_row();
110
111 println!(
112 "Rows {} -> ? ( {} )",
113 sheet.max_row(),
114 // formatted_row.len(),
115 default_row_hight
116 );
117
118 let mut hights = Vec::<f64>::new();
119 for _ in 0..sheet.max_row() {
120 hights.push(default_row_hight);
121 }
122
123 let widths = find_col_width(sheet)?;
124 let bounds = "|===\r";
125 let line = tab_header(&widths);
126
127 let mut output_file = File::create(out_file_name)?; // overwrites existing file
128 let mut writer = BufWriter::new(&mut output_file);
129
130 writer.write(line.as_bytes())?;
131 writer.write(bounds.as_bytes())?;
132
133 /* // todo test
134 writer.write("|1|2|3\r".as_bytes())?;
135 writer.write("|4|5|6\r".as_bytes())?;
136
137 writer.write(bounds.as_bytes())?;
138 */
139 for row in 0..sheet.max_row() {
140 println!("Row {} ({})", row, hights[row as usize]);
141 for col in 0..sheet.max_column() {
142 if col < sheet.max_column() {
143 writer.write("|".as_bytes())?;
144 }
145
146 let cell_content = sheet.read_cell((row + 1, col + 1)).unwrap_or_default();
147 let format = cell_content.format;
148 let mut text_color = FormatColor::Default;
149 let mut bg_color = FormatColor::Default;
150 let mut bg_bg_color = FormatColor::Default;
151 if format.is_some() {
152 let format = format.unwrap();
153 text_color = format.get_color().clone();
154 let ff = format.get_background().clone();
155 bg_color = ff.fg_color.clone();
156 bg_bg_color = ff.bg_color.clone();
157 }
158
159 let cell_format_string = format!(
160 "Text-Color = {:?} bg = {:?} bg_bg = {:?}",
161 text_color, bg_color, bg_bg_color
162 );
163 let cell_text = cell_content.text;
164 let text = match cell_text {
165 Some(t) => t,
166 None => "-".to_owned(),
167 };
168
169 println!(
170 "{} ({}) -> {} Format: {}",
171 col,
172 widths[(col) as usize],
173 text,
174 cell_format_string
175 );
176 writer.write(text.as_bytes())?;
177 }
178
179 writer.write("\r".as_bytes())?;
180 }
181 writer.write(bounds.as_bytes())?;
182
183 let xlsx_2_adoc_test_results = Xlsx2AdocTestResults { v1: 0, v2: 0 };
184 Ok(xlsx_2_adoc_test_results)
185}More examples
examples/read_and_copy.rs (line 44)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}Sourcepub fn max_row(&self) -> u32
pub fn max_row(&self) -> u32
Examples found in repository?
examples/read_and_copy.rs (line 32)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}More examples
examples/xlsx2adoc.rs (line 113)
99pub fn xlsx_convert(
100 in_file_name: &Path,
101 out_file_name: &Path,
102) -> Result<Xlsx2AdocTestResults, Error> {
103 let workbook = Workbook::from_path(in_file_name);
104 let mut workbook = workbook.unwrap();
105 workbook.finish();
106
107 let reading_sheet = workbook.get_worksheet(1);
108 let sheet = reading_sheet.unwrap();
109 let default_row_hight = sheet.get_default_row();
110
111 println!(
112 "Rows {} -> ? ( {} )",
113 sheet.max_row(),
114 // formatted_row.len(),
115 default_row_hight
116 );
117
118 let mut hights = Vec::<f64>::new();
119 for _ in 0..sheet.max_row() {
120 hights.push(default_row_hight);
121 }
122
123 let widths = find_col_width(sheet)?;
124 let bounds = "|===\r";
125 let line = tab_header(&widths);
126
127 let mut output_file = File::create(out_file_name)?; // overwrites existing file
128 let mut writer = BufWriter::new(&mut output_file);
129
130 writer.write(line.as_bytes())?;
131 writer.write(bounds.as_bytes())?;
132
133 /* // todo test
134 writer.write("|1|2|3\r".as_bytes())?;
135 writer.write("|4|5|6\r".as_bytes())?;
136
137 writer.write(bounds.as_bytes())?;
138 */
139 for row in 0..sheet.max_row() {
140 println!("Row {} ({})", row, hights[row as usize]);
141 for col in 0..sheet.max_column() {
142 if col < sheet.max_column() {
143 writer.write("|".as_bytes())?;
144 }
145
146 let cell_content = sheet.read_cell((row + 1, col + 1)).unwrap_or_default();
147 let format = cell_content.format;
148 let mut text_color = FormatColor::Default;
149 let mut bg_color = FormatColor::Default;
150 let mut bg_bg_color = FormatColor::Default;
151 if format.is_some() {
152 let format = format.unwrap();
153 text_color = format.get_color().clone();
154 let ff = format.get_background().clone();
155 bg_color = ff.fg_color.clone();
156 bg_bg_color = ff.bg_color.clone();
157 }
158
159 let cell_format_string = format!(
160 "Text-Color = {:?} bg = {:?} bg_bg = {:?}",
161 text_color, bg_color, bg_bg_color
162 );
163 let cell_text = cell_content.text;
164 let text = match cell_text {
165 Some(t) => t,
166 None => "-".to_owned(),
167 };
168
169 println!(
170 "{} ({}) -> {} Format: {}",
171 col,
172 widths[(col) as usize],
173 text,
174 cell_format_string
175 );
176 writer.write(text.as_bytes())?;
177 }
178
179 writer.write("\r".as_bytes())?;
180 }
181 writer.write(bounds.as_bytes())?;
182
183 let xlsx_2_adoc_test_results = Xlsx2AdocTestResults { v1: 0, v2: 0 };
184 Ok(xlsx_2_adoc_test_results)
185}pub fn get_name(&self) -> &str
Sourcepub fn set_name(&mut self, name: &str) -> WorkSheetResult<()>
pub fn set_name(&mut self, name: &str) -> WorkSheetResult<()>
Examples found in repository?
examples/duplicate_sheet.rs (line 9)
3fn main() -> WorkbookResult<()> {
4 // from an existed workbook
5 let mut workbook = Workbook::from_path("examples/xlsx/accounting.xlsx")?;
6 // Use the first worksheet as a template
7 let template = workbook.get_worksheet_mut(1)?;
8 template.insert_image("I1:L3", &"./examples/pics/ferris.png");
9 template.set_name("template")?;
10 // Deselect and hide the template.
11 template.deselect();
12 template.hide();
13 //
14 // Example of using the duplicate_worksheet() function.
15 //
16 let jan = workbook.duplicate_worksheet(1)?;
17 jan.write("A1", "Accounting Journal in Jan.")?;
18 jan.set_name("Jan.")?;
19 for row in 6..=15 {
20 jan.write_row((row, 3), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
21 }
22 let feb = workbook.duplicate_worksheet(1)?;
23 feb.write("A1", "Accounting Journal in Feb.")?;
24 feb.set_name("Feb.")?;
25 for row in 6..=15 {
26 feb.write_row((row, 3), &[2, 4, 6, 8, 10, 12, 14, 16, 18, 20])?;
27 }
28 // activate the Feb. sheet.
29 feb.activate();
30 // duplicate worksheet by sheet name
31 let mar = workbook.duplicate_worksheet_by_name("template")?;
32 mar.write("A1", "Accounting Journal in Mar.")?;
33 mar.set_name("Mar.")?;
34 for col in 'C'..='L' {
35 mar.write_column(&format!("{col}6"), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
36 }
37 workbook.save_as("examples/duplicate_sheet.xlsx")?;
38 Ok(())
39}Sourcepub fn activate(&mut self)
pub fn activate(&mut self)
Examples found in repository?
examples/duplicate_sheet.rs (line 29)
3fn main() -> WorkbookResult<()> {
4 // from an existed workbook
5 let mut workbook = Workbook::from_path("examples/xlsx/accounting.xlsx")?;
6 // Use the first worksheet as a template
7 let template = workbook.get_worksheet_mut(1)?;
8 template.insert_image("I1:L3", &"./examples/pics/ferris.png");
9 template.set_name("template")?;
10 // Deselect and hide the template.
11 template.deselect();
12 template.hide();
13 //
14 // Example of using the duplicate_worksheet() function.
15 //
16 let jan = workbook.duplicate_worksheet(1)?;
17 jan.write("A1", "Accounting Journal in Jan.")?;
18 jan.set_name("Jan.")?;
19 for row in 6..=15 {
20 jan.write_row((row, 3), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
21 }
22 let feb = workbook.duplicate_worksheet(1)?;
23 feb.write("A1", "Accounting Journal in Feb.")?;
24 feb.set_name("Feb.")?;
25 for row in 6..=15 {
26 feb.write_row((row, 3), &[2, 4, 6, 8, 10, 12, 14, 16, 18, 20])?;
27 }
28 // activate the Feb. sheet.
29 feb.activate();
30 // duplicate worksheet by sheet name
31 let mar = workbook.duplicate_worksheet_by_name("template")?;
32 mar.write("A1", "Accounting Journal in Mar.")?;
33 mar.set_name("Mar.")?;
34 for col in 'C'..='L' {
35 mar.write_column(&format!("{col}6"), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
36 }
37 workbook.save_as("examples/duplicate_sheet.xlsx")?;
38 Ok(())
39}pub fn select(&mut self)
Sourcepub fn deselect(&mut self)
pub fn deselect(&mut self)
Examples found in repository?
examples/duplicate_sheet.rs (line 11)
3fn main() -> WorkbookResult<()> {
4 // from an existed workbook
5 let mut workbook = Workbook::from_path("examples/xlsx/accounting.xlsx")?;
6 // Use the first worksheet as a template
7 let template = workbook.get_worksheet_mut(1)?;
8 template.insert_image("I1:L3", &"./examples/pics/ferris.png");
9 template.set_name("template")?;
10 // Deselect and hide the template.
11 template.deselect();
12 template.hide();
13 //
14 // Example of using the duplicate_worksheet() function.
15 //
16 let jan = workbook.duplicate_worksheet(1)?;
17 jan.write("A1", "Accounting Journal in Jan.")?;
18 jan.set_name("Jan.")?;
19 for row in 6..=15 {
20 jan.write_row((row, 3), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
21 }
22 let feb = workbook.duplicate_worksheet(1)?;
23 feb.write("A1", "Accounting Journal in Feb.")?;
24 feb.set_name("Feb.")?;
25 for row in 6..=15 {
26 feb.write_row((row, 3), &[2, 4, 6, 8, 10, 12, 14, 16, 18, 20])?;
27 }
28 // activate the Feb. sheet.
29 feb.activate();
30 // duplicate worksheet by sheet name
31 let mar = workbook.duplicate_worksheet_by_name("template")?;
32 mar.write("A1", "Accounting Journal in Mar.")?;
33 mar.set_name("Mar.")?;
34 for col in 'C'..='L' {
35 mar.write_column(&format!("{col}6"), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
36 }
37 workbook.save_as("examples/duplicate_sheet.xlsx")?;
38 Ok(())
39}Sourcepub fn right_to_left(&mut self)
pub fn right_to_left(&mut self)
Examples found in repository?
examples/right_to_left.rs (line 25)
3fn main() -> WorkbookResult<()> {
4 // Add the cell formats.
5 let format_left_to_right = Format::default()
6 .set_reading_order(1);
7 let format_right_to_left = Format::default()
8 .set_reading_order(2);
9
10 // Create a new workbook
11 let mut workbook = Workbook::new();
12 // Set up some worksheets and set tab colors
13 let worksheet1 = workbook.get_worksheet_mut(1)?;
14
15 // Make the columns wider for clarity.
16 worksheet1.set_columns_width("A:A", 25.0)?;
17
18 // Standard direction: | A1 | B1 | C1 | ...
19 worksheet1.write("A1", "نص عربي / English text")?; // Default direction.
20 worksheet1.write_with_format("A2", "نص عربي / English text", &format_left_to_right)?;
21 worksheet1.write_with_format("A3", "نص عربي / English text", &format_right_to_left)?;
22
23 let worksheet2 = workbook.add_worksheet()?;
24 worksheet2.set_columns_width("A:A", 25.0)?;
25 worksheet2.right_to_left();
26
27 // Right to left direction: ... | C1 | B1 | A1 |
28 worksheet2.write("A1", "نص عربي / English text")?; // Default direction.
29 worksheet2.write_with_format("A2", "نص عربي / English text", &format_left_to_right)?;
30 worksheet2.write_with_format("A3", "نص عربي / English text", &format_right_to_left)?;
31
32 workbook.save_as("examples/right_to_left.xlsx")?;
33
34 Ok(())
35}pub fn set_top_left_cell<L: Location>(&mut self, loc: L)
pub fn set_zoom(&mut self, zoom_scale: u16)
Sourcepub fn set_selection<L: LocationRange>(
&mut self,
loc_range: L,
) -> WorkSheetResult<()>
pub fn set_selection<L: LocationRange>( &mut self, loc_range: L, ) -> WorkSheetResult<()>
Examples found in repository?
examples/panes.rs (line 23)
3fn main() -> WorkbookResult<()> {
4 let header_format = Format::default()
5 .set_bold()
6 .set_align(FormatAlignType::Center)
7 .set_align(FormatAlignType::VerticalCenter)
8 .set_border(FormatBorderType::Medium)
9 .set_background_color(FormatColor::RGB(126, 75, 12));
10 let center_format = Format::default().set_align(FormatAlignType::Center);
11
12 // Create a new workbook
13 let mut workbook = Workbook::new();
14
15 //
16 // Example 1. Freeze pane on the top row.
17 //
18 let worksheet1 = workbook.add_worksheet_by_name("Panes 1")?;
19 worksheet1.freeze_panes("A2")?;
20 // Other sheet formatting.
21 worksheet1.set_columns_width("A:I", 16.0)?;
22 worksheet1.set_row_height(0, 20.0)?;
23 worksheet1.set_selection("C3:C3")?;
24
25 // Some text to demonstrate scrolling.
26 for col in 1..=9 {
27 worksheet1.write_with_format((1, col), "Scroll down", &header_format)?;
28 }
29
30 for row in 2..100 {
31 for col in 1..=9 {
32 worksheet1.write_with_format((row, col), row, ¢er_format)?;
33 }
34 }
35
36 //
37 // Example 2. Freeze pane on the left column.
38 //
39 let worksheet2 = workbook.add_worksheet_by_name("Panes 2")?;
40 worksheet2.freeze_panes("B1")?;
41
42 // Other sheet formatting.
43 worksheet2.set_columns_width("A:A", 16.0)?;
44 worksheet2.set_selection("C3:C3")?;
45
46 // Some text to demonstrate scrolling.
47 for row in 1..=50 {
48 worksheet2.write_with_format((row, 1), "Scroll right", &header_format)?;
49 for col in 2..=26 {
50 worksheet2.write_with_format((row, col), col, ¢er_format)?;
51 }
52 }
53
54 //
55 // Example 3. Freeze pane on the top row and left column.
56 //
57 let worksheet3 = workbook.add_worksheet_by_name("Panes 3")?;
58 worksheet3.freeze_panes((2, 2))?;
59
60 // Other sheet formatting.
61 worksheet3.set_columns_width("A:Z", 16.0)?;
62 worksheet3.set_row_height(1, 20.0)?;
63 worksheet3.set_selection("C3:C3")?;
64 worksheet3.write_with_format((1, 1), "", &header_format)?;
65
66 // Some text to demonstrate scrolling.
67 for col in 2..=26 {
68 worksheet3.write_with_format((1, col), "Scroll down", &header_format)?;
69 }
70
71 for row in 2..=50 {
72 worksheet3.write_with_format((row, 1), "Scroll right", &header_format)?;
73 for col in 2..=26 {
74 worksheet3.write_with_format((row, col), col, ¢er_format)?;
75 }
76 }
77
78 //
79 // Example 4. Split pane on the top row and left column.
80 //
81 // The divisions must be specified in terms of row and column dimensions.
82 //
83 let worksheet4 = workbook.add_worksheet_by_name("Panes 4")?;
84 // Set the default row height is 17 and set the column width is 13
85 worksheet4.set_columns_width("A:Z", 13.0)?;
86 worksheet4.set_default_row(17.0);
87 worksheet4.split_panes(2.0 * 13.0, 2.0 * 17.0)?;
88 worksheet4.set_selection("C3:C3")?;
89
90 // Some text to demonstrate scrolling.
91 for col in 1..=26 {
92 worksheet4.write_with_format((1, col), "Scroll", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}Sourcepub fn freeze_panes<L: Location>(&mut self, loc: L) -> WorkSheetResult<()>
pub fn freeze_panes<L: Location>(&mut self, loc: L) -> WorkSheetResult<()>
Examples found in repository?
examples/panes.rs (line 19)
3fn main() -> WorkbookResult<()> {
4 let header_format = Format::default()
5 .set_bold()
6 .set_align(FormatAlignType::Center)
7 .set_align(FormatAlignType::VerticalCenter)
8 .set_border(FormatBorderType::Medium)
9 .set_background_color(FormatColor::RGB(126, 75, 12));
10 let center_format = Format::default().set_align(FormatAlignType::Center);
11
12 // Create a new workbook
13 let mut workbook = Workbook::new();
14
15 //
16 // Example 1. Freeze pane on the top row.
17 //
18 let worksheet1 = workbook.add_worksheet_by_name("Panes 1")?;
19 worksheet1.freeze_panes("A2")?;
20 // Other sheet formatting.
21 worksheet1.set_columns_width("A:I", 16.0)?;
22 worksheet1.set_row_height(0, 20.0)?;
23 worksheet1.set_selection("C3:C3")?;
24
25 // Some text to demonstrate scrolling.
26 for col in 1..=9 {
27 worksheet1.write_with_format((1, col), "Scroll down", &header_format)?;
28 }
29
30 for row in 2..100 {
31 for col in 1..=9 {
32 worksheet1.write_with_format((row, col), row, ¢er_format)?;
33 }
34 }
35
36 //
37 // Example 2. Freeze pane on the left column.
38 //
39 let worksheet2 = workbook.add_worksheet_by_name("Panes 2")?;
40 worksheet2.freeze_panes("B1")?;
41
42 // Other sheet formatting.
43 worksheet2.set_columns_width("A:A", 16.0)?;
44 worksheet2.set_selection("C3:C3")?;
45
46 // Some text to demonstrate scrolling.
47 for row in 1..=50 {
48 worksheet2.write_with_format((row, 1), "Scroll right", &header_format)?;
49 for col in 2..=26 {
50 worksheet2.write_with_format((row, col), col, ¢er_format)?;
51 }
52 }
53
54 //
55 // Example 3. Freeze pane on the top row and left column.
56 //
57 let worksheet3 = workbook.add_worksheet_by_name("Panes 3")?;
58 worksheet3.freeze_panes((2, 2))?;
59
60 // Other sheet formatting.
61 worksheet3.set_columns_width("A:Z", 16.0)?;
62 worksheet3.set_row_height(1, 20.0)?;
63 worksheet3.set_selection("C3:C3")?;
64 worksheet3.write_with_format((1, 1), "", &header_format)?;
65
66 // Some text to demonstrate scrolling.
67 for col in 2..=26 {
68 worksheet3.write_with_format((1, col), "Scroll down", &header_format)?;
69 }
70
71 for row in 2..=50 {
72 worksheet3.write_with_format((row, 1), "Scroll right", &header_format)?;
73 for col in 2..=26 {
74 worksheet3.write_with_format((row, col), col, ¢er_format)?;
75 }
76 }
77
78 //
79 // Example 4. Split pane on the top row and left column.
80 //
81 // The divisions must be specified in terms of row and column dimensions.
82 //
83 let worksheet4 = workbook.add_worksheet_by_name("Panes 4")?;
84 // Set the default row height is 17 and set the column width is 13
85 worksheet4.set_columns_width("A:Z", 13.0)?;
86 worksheet4.set_default_row(17.0);
87 worksheet4.split_panes(2.0 * 13.0, 2.0 * 17.0)?;
88 worksheet4.set_selection("C3:C3")?;
89
90 // Some text to demonstrate scrolling.
91 for col in 1..=26 {
92 worksheet4.write_with_format((1, col), "Scroll", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}Sourcepub fn split_panes(&mut self, width: f64, height: f64) -> WorkSheetResult<()>
pub fn split_panes(&mut self, width: f64, height: f64) -> WorkSheetResult<()>
Examples found in repository?
examples/panes.rs (line 87)
3fn main() -> WorkbookResult<()> {
4 let header_format = Format::default()
5 .set_bold()
6 .set_align(FormatAlignType::Center)
7 .set_align(FormatAlignType::VerticalCenter)
8 .set_border(FormatBorderType::Medium)
9 .set_background_color(FormatColor::RGB(126, 75, 12));
10 let center_format = Format::default().set_align(FormatAlignType::Center);
11
12 // Create a new workbook
13 let mut workbook = Workbook::new();
14
15 //
16 // Example 1. Freeze pane on the top row.
17 //
18 let worksheet1 = workbook.add_worksheet_by_name("Panes 1")?;
19 worksheet1.freeze_panes("A2")?;
20 // Other sheet formatting.
21 worksheet1.set_columns_width("A:I", 16.0)?;
22 worksheet1.set_row_height(0, 20.0)?;
23 worksheet1.set_selection("C3:C3")?;
24
25 // Some text to demonstrate scrolling.
26 for col in 1..=9 {
27 worksheet1.write_with_format((1, col), "Scroll down", &header_format)?;
28 }
29
30 for row in 2..100 {
31 for col in 1..=9 {
32 worksheet1.write_with_format((row, col), row, ¢er_format)?;
33 }
34 }
35
36 //
37 // Example 2. Freeze pane on the left column.
38 //
39 let worksheet2 = workbook.add_worksheet_by_name("Panes 2")?;
40 worksheet2.freeze_panes("B1")?;
41
42 // Other sheet formatting.
43 worksheet2.set_columns_width("A:A", 16.0)?;
44 worksheet2.set_selection("C3:C3")?;
45
46 // Some text to demonstrate scrolling.
47 for row in 1..=50 {
48 worksheet2.write_with_format((row, 1), "Scroll right", &header_format)?;
49 for col in 2..=26 {
50 worksheet2.write_with_format((row, col), col, ¢er_format)?;
51 }
52 }
53
54 //
55 // Example 3. Freeze pane on the top row and left column.
56 //
57 let worksheet3 = workbook.add_worksheet_by_name("Panes 3")?;
58 worksheet3.freeze_panes((2, 2))?;
59
60 // Other sheet formatting.
61 worksheet3.set_columns_width("A:Z", 16.0)?;
62 worksheet3.set_row_height(1, 20.0)?;
63 worksheet3.set_selection("C3:C3")?;
64 worksheet3.write_with_format((1, 1), "", &header_format)?;
65
66 // Some text to demonstrate scrolling.
67 for col in 2..=26 {
68 worksheet3.write_with_format((1, col), "Scroll down", &header_format)?;
69 }
70
71 for row in 2..=50 {
72 worksheet3.write_with_format((row, 1), "Scroll right", &header_format)?;
73 for col in 2..=26 {
74 worksheet3.write_with_format((row, col), col, ¢er_format)?;
75 }
76 }
77
78 //
79 // Example 4. Split pane on the top row and left column.
80 //
81 // The divisions must be specified in terms of row and column dimensions.
82 //
83 let worksheet4 = workbook.add_worksheet_by_name("Panes 4")?;
84 // Set the default row height is 17 and set the column width is 13
85 worksheet4.set_columns_width("A:Z", 13.0)?;
86 worksheet4.set_default_row(17.0);
87 worksheet4.split_panes(2.0 * 13.0, 2.0 * 17.0)?;
88 worksheet4.set_selection("C3:C3")?;
89
90 // Some text to demonstrate scrolling.
91 for col in 1..=26 {
92 worksheet4.write_with_format((1, col), "Scroll", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}Sourcepub fn set_default_row(&mut self, height: f64)
pub fn set_default_row(&mut self, height: f64)
Examples found in repository?
examples/read_and_copy.rs (line 31)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}More examples
examples/panes.rs (line 86)
3fn main() -> WorkbookResult<()> {
4 let header_format = Format::default()
5 .set_bold()
6 .set_align(FormatAlignType::Center)
7 .set_align(FormatAlignType::VerticalCenter)
8 .set_border(FormatBorderType::Medium)
9 .set_background_color(FormatColor::RGB(126, 75, 12));
10 let center_format = Format::default().set_align(FormatAlignType::Center);
11
12 // Create a new workbook
13 let mut workbook = Workbook::new();
14
15 //
16 // Example 1. Freeze pane on the top row.
17 //
18 let worksheet1 = workbook.add_worksheet_by_name("Panes 1")?;
19 worksheet1.freeze_panes("A2")?;
20 // Other sheet formatting.
21 worksheet1.set_columns_width("A:I", 16.0)?;
22 worksheet1.set_row_height(0, 20.0)?;
23 worksheet1.set_selection("C3:C3")?;
24
25 // Some text to demonstrate scrolling.
26 for col in 1..=9 {
27 worksheet1.write_with_format((1, col), "Scroll down", &header_format)?;
28 }
29
30 for row in 2..100 {
31 for col in 1..=9 {
32 worksheet1.write_with_format((row, col), row, ¢er_format)?;
33 }
34 }
35
36 //
37 // Example 2. Freeze pane on the left column.
38 //
39 let worksheet2 = workbook.add_worksheet_by_name("Panes 2")?;
40 worksheet2.freeze_panes("B1")?;
41
42 // Other sheet formatting.
43 worksheet2.set_columns_width("A:A", 16.0)?;
44 worksheet2.set_selection("C3:C3")?;
45
46 // Some text to demonstrate scrolling.
47 for row in 1..=50 {
48 worksheet2.write_with_format((row, 1), "Scroll right", &header_format)?;
49 for col in 2..=26 {
50 worksheet2.write_with_format((row, col), col, ¢er_format)?;
51 }
52 }
53
54 //
55 // Example 3. Freeze pane on the top row and left column.
56 //
57 let worksheet3 = workbook.add_worksheet_by_name("Panes 3")?;
58 worksheet3.freeze_panes((2, 2))?;
59
60 // Other sheet formatting.
61 worksheet3.set_columns_width("A:Z", 16.0)?;
62 worksheet3.set_row_height(1, 20.0)?;
63 worksheet3.set_selection("C3:C3")?;
64 worksheet3.write_with_format((1, 1), "", &header_format)?;
65
66 // Some text to demonstrate scrolling.
67 for col in 2..=26 {
68 worksheet3.write_with_format((1, col), "Scroll down", &header_format)?;
69 }
70
71 for row in 2..=50 {
72 worksheet3.write_with_format((row, 1), "Scroll right", &header_format)?;
73 for col in 2..=26 {
74 worksheet3.write_with_format((row, col), col, ¢er_format)?;
75 }
76 }
77
78 //
79 // Example 4. Split pane on the top row and left column.
80 //
81 // The divisions must be specified in terms of row and column dimensions.
82 //
83 let worksheet4 = workbook.add_worksheet_by_name("Panes 4")?;
84 // Set the default row height is 17 and set the column width is 13
85 worksheet4.set_columns_width("A:Z", 13.0)?;
86 worksheet4.set_default_row(17.0);
87 worksheet4.split_panes(2.0 * 13.0, 2.0 * 17.0)?;
88 worksheet4.set_selection("C3:C3")?;
89
90 // Some text to demonstrate scrolling.
91 for col in 1..=26 {
92 worksheet4.write_with_format((1, col), "Scroll", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}Sourcepub fn get_default_row(&self) -> f64
pub fn get_default_row(&self) -> f64
Examples found in repository?
examples/read_and_copy.rs (line 31)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}More examples
examples/xlsx2adoc.rs (line 109)
99pub fn xlsx_convert(
100 in_file_name: &Path,
101 out_file_name: &Path,
102) -> Result<Xlsx2AdocTestResults, Error> {
103 let workbook = Workbook::from_path(in_file_name);
104 let mut workbook = workbook.unwrap();
105 workbook.finish();
106
107 let reading_sheet = workbook.get_worksheet(1);
108 let sheet = reading_sheet.unwrap();
109 let default_row_hight = sheet.get_default_row();
110
111 println!(
112 "Rows {} -> ? ( {} )",
113 sheet.max_row(),
114 // formatted_row.len(),
115 default_row_hight
116 );
117
118 let mut hights = Vec::<f64>::new();
119 for _ in 0..sheet.max_row() {
120 hights.push(default_row_hight);
121 }
122
123 let widths = find_col_width(sheet)?;
124 let bounds = "|===\r";
125 let line = tab_header(&widths);
126
127 let mut output_file = File::create(out_file_name)?; // overwrites existing file
128 let mut writer = BufWriter::new(&mut output_file);
129
130 writer.write(line.as_bytes())?;
131 writer.write(bounds.as_bytes())?;
132
133 /* // todo test
134 writer.write("|1|2|3\r".as_bytes())?;
135 writer.write("|4|5|6\r".as_bytes())?;
136
137 writer.write(bounds.as_bytes())?;
138 */
139 for row in 0..sheet.max_row() {
140 println!("Row {} ({})", row, hights[row as usize]);
141 for col in 0..sheet.max_column() {
142 if col < sheet.max_column() {
143 writer.write("|".as_bytes())?;
144 }
145
146 let cell_content = sheet.read_cell((row + 1, col + 1)).unwrap_or_default();
147 let format = cell_content.format;
148 let mut text_color = FormatColor::Default;
149 let mut bg_color = FormatColor::Default;
150 let mut bg_bg_color = FormatColor::Default;
151 if format.is_some() {
152 let format = format.unwrap();
153 text_color = format.get_color().clone();
154 let ff = format.get_background().clone();
155 bg_color = ff.fg_color.clone();
156 bg_bg_color = ff.bg_color.clone();
157 }
158
159 let cell_format_string = format!(
160 "Text-Color = {:?} bg = {:?} bg_bg = {:?}",
161 text_color, bg_color, bg_bg_color
162 );
163 let cell_text = cell_content.text;
164 let text = match cell_text {
165 Some(t) => t,
166 None => "-".to_owned(),
167 };
168
169 println!(
170 "{} ({}) -> {} Format: {}",
171 col,
172 widths[(col) as usize],
173 text,
174 cell_format_string
175 );
176 writer.write(text.as_bytes())?;
177 }
178
179 writer.write("\r".as_bytes())?;
180 }
181 writer.write(bounds.as_bytes())?;
182
183 let xlsx_2_adoc_test_results = Xlsx2AdocTestResults { v1: 0, v2: 0 };
184 Ok(xlsx_2_adoc_test_results)
185}Sourcepub fn set_default_column(&mut self, height: f64)
pub fn set_default_column(&mut self, height: f64)
Examples found in repository?
examples/read_and_copy.rs (line 19)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}Sourcepub fn set_default_column_adaptive(&mut self)
pub fn set_default_column_adaptive(&mut self)
Examples found in repository?
examples/read_and_copy.rs (line 18)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}Sourcepub fn get_default_column(&self) -> Option<f64>
pub fn get_default_column(&self) -> Option<f64>
Examples found in repository?
examples/xlsx2adoc.rs (line 69)
67fn find_col_width(sheet: &WorkSheet) -> Result<Vec<f64>, Error> {
68 let mut widths = Vec::<f64>::new();
69 let default_col_width = sheet.get_default_column().unwrap_or(1.0);
70
71 for _ in 0..sheet.max_column() {
72 widths.push(default_col_width);
73 }
74
75 let formatted_col_result = sheet.get_columns_with_format((1, 1, 1, 16384));
76 let formatted_col = match formatted_col_result {
77 Ok(f) => f,
78 Err(e) => return Err(error_text(&format!("{:?}", e))),
79 };
80
81 for w in formatted_col.iter() {
82 let column_name = w.0;
83 let a = w.1;
84 let columns_specs = a.0;
85 let column_width = columns_specs.width;
86 match column_width {
87 Some(width) => {
88 let col_range = decode_col_range(column_name, widths.len());
89 for c in col_range {
90 widths[c] = width;
91 }
92 }
93 None => {},
94 };
95}
96 Ok(widths)
97}More examples
examples/read_and_copy.rs (line 17)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}Sourcepub fn hide_unused_rows(&mut self, hide: bool)
pub fn hide_unused_rows(&mut self, hide: bool)
Examples found in repository?
examples/hide_row_col.rs (line 14)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6
7 let worksheet = workbook.get_worksheet_mut(1)?;
8
9 // Write some data.
10 worksheet.write("D1", "Some hidden columns.")?;
11 worksheet.write("A8", "Some hidden rows.")?;
12
13 // Hide all rows without data.
14 worksheet.hide_unused_rows(true);
15
16 // Set the height of empty rows that we do want to display even if it is
17 // the default height.
18 for row in 2..=7 {
19 worksheet.set_row_height(row, 15.0)?;
20 }
21
22 // Columns can be hidden explicitly. This doesn't increase the file size..
23 worksheet.hide_columns("G:XFD")?;
24
25 workbook.save_as("examples/hide_row_col.xlsx")?;
26 Ok(())
27}pub fn outline_settings( &mut self, visible: bool, symbols_below: bool, symbols_right: bool, auto_style: bool, )
Sourcepub fn ignore_errors<L: Location>(&mut self, error_map: HashMap<&str, L>)
pub fn ignore_errors<L: Location>(&mut self, error_map: HashMap<&str, L>)
Examples found in repository?
examples/ignore_errors.rs (line 23)
4fn main() -> WorkbookResult<()> {
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7
8 // Write strings that looks like numbers. This will cause an Excel warning.
9 worksheet.write_string("C2", "123".to_string())?;
10 worksheet.write_string("C3", "123".to_string())?;
11
12 // Write a divide by zero formula. This will also cause an Excel warning.
13 worksheet.write_formula("C5", "=1/0")?;
14 worksheet.write_formula("C6", "=1/0")?;
15 // In older versions of Excel, you could use the write_old_formula method:
16 // worksheet.write_old_formula("C5", "=1/0")?;
17 // worksheet.write_old_formula("C6", "=1/0")?;
18
19 // Turn off some of the warnings:
20 let mut error_map = HashMap::new();
21 error_map.insert("number_stored_as_text", "C3");
22 error_map.insert("eval_error", "C6");
23 worksheet.ignore_errors(error_map);
24
25 // Write some descriptions for the cells and make the column wider for clarity.
26 worksheet.set_columns_width("B:B", 16.0)?;
27 worksheet.write("B2", "Warning:")?;
28 worksheet.write("B3", "Warning turned off:")?;
29 worksheet.write("B5", "Warning:")?;
30 worksheet.write("B6", "Warning turned off:")?;
31
32 workbook.save_as("examples/ignore_errors.xlsx")?;
33 Ok(())
34}Sourcepub fn hide(&mut self)
pub fn hide(&mut self)
Examples found in repository?
examples/hide_sheet.rs (line 15)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet1 = workbook.get_worksheet_mut(1)?;
7
8 worksheet1.set_columns_width("A:A", 30.0)?;
9 worksheet1.write("A1", "Sheet2 is hidden")?;
10
11 // Hide Sheet2. It won't be visible until it is unhidden in Excel.
12 let worksheet2 = workbook.add_worksheet()?;
13 worksheet2.set_columns_width("A:A", 30.0)?;
14 // worksheet2.activate();
15 worksheet2.hide();
16 worksheet2.write("A1", "Now it's my turn to find you!")?;
17 // Note, you can't hide the "active" worksheet, which generally is the
18 // first worksheet, since this would cause an Excel error. So, in order to hide
19 // the first sheet you will need to activate another worksheet:
20 //
21 // worksheet2.activate();
22 // worksheet1.hide();
23
24 let worksheet3 = workbook.add_worksheet()?;
25 worksheet3.set_columns_width("A:A", 30.0)?;
26 worksheet3.write("A1", "Sheet2 is hidden")?;
27
28 workbook.save_as("examples/hide_sheet.xlsx")?;
29 Ok(())
30}More examples
examples/duplicate_sheet.rs (line 12)
3fn main() -> WorkbookResult<()> {
4 // from an existed workbook
5 let mut workbook = Workbook::from_path("examples/xlsx/accounting.xlsx")?;
6 // Use the first worksheet as a template
7 let template = workbook.get_worksheet_mut(1)?;
8 template.insert_image("I1:L3", &"./examples/pics/ferris.png");
9 template.set_name("template")?;
10 // Deselect and hide the template.
11 template.deselect();
12 template.hide();
13 //
14 // Example of using the duplicate_worksheet() function.
15 //
16 let jan = workbook.duplicate_worksheet(1)?;
17 jan.write("A1", "Accounting Journal in Jan.")?;
18 jan.set_name("Jan.")?;
19 for row in 6..=15 {
20 jan.write_row((row, 3), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
21 }
22 let feb = workbook.duplicate_worksheet(1)?;
23 feb.write("A1", "Accounting Journal in Feb.")?;
24 feb.set_name("Feb.")?;
25 for row in 6..=15 {
26 feb.write_row((row, 3), &[2, 4, 6, 8, 10, 12, 14, 16, 18, 20])?;
27 }
28 // activate the Feb. sheet.
29 feb.activate();
30 // duplicate worksheet by sheet name
31 let mar = workbook.duplicate_worksheet_by_name("template")?;
32 mar.write("A1", "Accounting Journal in Mar.")?;
33 mar.set_name("Mar.")?;
34 for col in 'C'..='L' {
35 mar.write_column(&format!("{col}6"), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
36 }
37 workbook.save_as("examples/duplicate_sheet.xlsx")?;
38 Ok(())
39}Sourcepub fn set_tab_color(&mut self, tab_color: &FormatColor)
pub fn set_tab_color(&mut self, tab_color: &FormatColor)
Examples found in repository?
examples/tab_color.rs (line 8)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 // Set up some worksheets and set tab colors
7 let worksheet1 = workbook.get_worksheet_mut(1)?;
8 worksheet1.set_tab_color(&FormatColor::RGB(255, 0, 0)); // Red
9 let worksheet2 = workbook.add_worksheet()?;
10 worksheet2.set_tab_color(&FormatColor::Index(11)); // Green
11 let worksheet3 = workbook.add_worksheet()?;
12 worksheet3.set_tab_color(&FormatColor::Theme(5, 0.0)); // Orange
13 let worksheet4 = workbook.add_worksheet()?;
14 // worksheet4 will have the default color.
15 workbook.save_as("examples/tab_color.xlsx")?;
16 Ok(())
17}More examples
examples/hello_world.rs (line 36)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}Sourcepub fn set_background<P: AsRef<Path>>(
&mut self,
filename: P,
) -> WorkSheetResult<()>
pub fn set_background<P: AsRef<Path>>( &mut self, filename: P, ) -> WorkSheetResult<()>
Examples found in repository?
examples/background.rs (line 10)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 // Use the default worksheet
7 let worksheet = workbook.get_worksheet_mut(1)?;
8 // Set the background image.
9 // **NOTICE**: Only png format is acceptable in the background image
10 worksheet.set_background("examples/pics/ferris.png")?;
11
12 workbook.save_as("examples/background.xlsx")?;
13 Ok(())
14}More examples
examples/hello_world.rs (line 38)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}Sourcepub fn insert_image<L: LocationRange, P: AsRef<Path>>(
&mut self,
loc_range: L,
filename: &P,
) -> WorkSheetResult<()>
pub fn insert_image<L: LocationRange, P: AsRef<Path>>( &mut self, loc_range: L, filename: &P, ) -> WorkSheetResult<()>
Examples found in repository?
More examples
examples/duplicate_sheet.rs (line 8)
3fn main() -> WorkbookResult<()> {
4 // from an existed workbook
5 let mut workbook = Workbook::from_path("examples/xlsx/accounting.xlsx")?;
6 // Use the first worksheet as a template
7 let template = workbook.get_worksheet_mut(1)?;
8 template.insert_image("I1:L3", &"./examples/pics/ferris.png");
9 template.set_name("template")?;
10 // Deselect and hide the template.
11 template.deselect();
12 template.hide();
13 //
14 // Example of using the duplicate_worksheet() function.
15 //
16 let jan = workbook.duplicate_worksheet(1)?;
17 jan.write("A1", "Accounting Journal in Jan.")?;
18 jan.set_name("Jan.")?;
19 for row in 6..=15 {
20 jan.write_row((row, 3), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
21 }
22 let feb = workbook.duplicate_worksheet(1)?;
23 feb.write("A1", "Accounting Journal in Feb.")?;
24 feb.set_name("Feb.")?;
25 for row in 6..=15 {
26 feb.write_row((row, 3), &[2, 4, 6, 8, 10, 12, 14, 16, 18, 20])?;
27 }
28 // activate the Feb. sheet.
29 feb.activate();
30 // duplicate worksheet by sheet name
31 let mar = workbook.duplicate_worksheet_by_name("template")?;
32 mar.write("A1", "Accounting Journal in Mar.")?;
33 mar.set_name("Mar.")?;
34 for col in 'C'..='L' {
35 mar.write_column(&format!("{col}6"), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
36 }
37 workbook.save_as("examples/duplicate_sheet.xlsx")?;
38 Ok(())
39}examples/hello_world.rs (line 49)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}pub fn id(&self) -> u32
Trait Implementations§
Source§impl WorkSheetCol for WorkSheet
impl WorkSheetCol for WorkSheet
Source§fn get_columns<R: LocationRange>(
&self,
col_range: R,
) -> WorkSheetResult<HashMap<String, Column>>
fn get_columns<R: LocationRange>( &self, col_range: R, ) -> WorkSheetResult<HashMap<String, Column>>
Source§fn get_columns_with_format<R: LocationRange>(
&self,
col_range: R,
) -> WorkSheetResult<HashMap<String, (Column, Option<Format>)>>
fn get_columns_with_format<R: LocationRange>( &self, col_range: R, ) -> WorkSheetResult<HashMap<String, (Column, Option<Format>)>>
Source§fn get_columns_width<R: LocationRange>(
&self,
col_range: R,
) -> WorkSheetResult<HashMap<String, Option<f64>>>
fn get_columns_width<R: LocationRange>( &self, col_range: R, ) -> WorkSheetResult<HashMap<String, Option<f64>>>
Get the custom width of cols based on the col range,
note that the col range starts with 1. Read more
Source§fn set_columns<R: LocationRange>(
&mut self,
col_range: R,
column: &Column,
) -> WorkSheetResult<()>
fn set_columns<R: LocationRange>( &mut self, col_range: R, column: &Column, ) -> WorkSheetResult<()>
Source§fn set_columns_with_format<R: LocationRange>(
&mut self,
col_range: R,
column: &Column,
format: &Format,
) -> WorkSheetResult<()>
fn set_columns_with_format<R: LocationRange>( &mut self, col_range: R, column: &Column, format: &Format, ) -> WorkSheetResult<()>
Source§fn set_columns_width<R: LocationRange>(
&mut self,
col_range: R,
width: f64,
) -> WorkSheetResult<()>
fn set_columns_width<R: LocationRange>( &mut self, col_range: R, width: f64, ) -> WorkSheetResult<()>
set the width of columns by columns range,
The effect is the same as Read more
fn set_columns_width_pixels<R: LocationRange>( &mut self, col_range: R, width: f64, ) -> WorkSheetResult<()>
Source§fn set_columns_width_with_format<R: LocationRange>(
&mut self,
col_range: R,
width: f64,
format: &Format,
) -> WorkSheetResult<()>
fn set_columns_width_with_format<R: LocationRange>( &mut self, col_range: R, width: f64, format: &Format, ) -> WorkSheetResult<()>
set the width of columns by columns range,
The effect is the same as Read more
fn set_columns_width_pixels_with_format<R: LocationRange>( &mut self, col_range: R, width: f64, format: &Format, ) -> WorkSheetResult<()>
Source§fn hide_columns<R: LocationRange>(
&mut self,
col_range: R,
) -> WorkSheetResult<()>
fn hide_columns<R: LocationRange>( &mut self, col_range: R, ) -> WorkSheetResult<()>
hide columns by column range,
The effect is the same as Read more
Source§fn set_columns_level<R: LocationRange>(
&mut self,
col_range: R,
level: u8,
) -> WorkSheetResult<()>
fn set_columns_level<R: LocationRange>( &mut self, col_range: R, level: u8, ) -> WorkSheetResult<()>
set the outline of columns by column range,
The effect is the same as Read more
Source§fn collapse_columns<R: LocationRange>(
&mut self,
col_range: R,
) -> WorkSheetResult<()>
fn collapse_columns<R: LocationRange>( &mut self, col_range: R, ) -> WorkSheetResult<()>
collapse columns by column range,
The effect is the same as Read more
Source§impl WorkSheetRow for WorkSheet
impl WorkSheetRow for WorkSheet
Source§fn get_row_with_format(
&self,
row: u32,
) -> WorkSheetResult<(Row, Option<Format>)>
fn get_row_with_format( &self, row: u32, ) -> WorkSheetResult<(Row, Option<Format>)>
Source§fn get_row_height(&self, row: u32) -> WorkSheetResult<Option<f64>>
fn get_row_height(&self, row: u32) -> WorkSheetResult<Option<f64>>
Get the custom height of a row based on the row number,
note that the row number starts with 1.
Only rows have custom height can return their height. Read more
Source§fn set_row_with_format(
&mut self,
row_number: u32,
row: &Row,
format: &Format,
) -> WorkSheetResult<()>
fn set_row_with_format( &mut self, row_number: u32, row: &Row, format: &Format, ) -> WorkSheetResult<()>
Source§fn set_row_height(&mut self, row: u32, height: f64) -> WorkSheetResult<()>
fn set_row_height(&mut self, row: u32, height: f64) -> WorkSheetResult<()>
set the height of a row by row number,
The effect is the same as Read more
fn set_row_height_pixels( &mut self, row: u32, height: f64, ) -> WorkSheetResult<()>
Source§fn set_row_height_with_format(
&mut self,
row: u32,
height: f64,
format: &Format,
) -> WorkSheetResult<()>
fn set_row_height_with_format( &mut self, row: u32, height: f64, format: &Format, ) -> WorkSheetResult<()>
set the height and format of a row by row number,
The effect is the same as Read more
fn set_row_height_pixels_with_format( &mut self, row: u32, height: f64, format: &Format, ) -> WorkSheetResult<()>
Source§fn hide_row(&mut self, row: u32) -> WorkSheetResult<()>
fn hide_row(&mut self, row: u32) -> WorkSheetResult<()>
hide a row by row number,
The effect is the same as Read more
Source§fn set_row_level(&mut self, row: u32, level: u8) -> WorkSheetResult<()>
fn set_row_level(&mut self, row: u32, level: u8) -> WorkSheetResult<()>
set the outline of a row by row number,
The effect is the same as Read more
Source§fn collapse_row(&mut self, row: u32) -> WorkSheetResult<()>
fn collapse_row(&mut self, row: u32) -> WorkSheetResult<()>
collapse a row by row number,
The effect is the same as Read more
Source§impl Write for WorkSheet
impl Write for WorkSheet
fn write_cell<L: Location, T: Clone + CellDisplay + CellValue>( &mut self, loc: L, cell: &Cell<T>, ) -> WorkSheetResult<()>
fn write<L: Location, T: Default + Clone + CellDisplay + CellValue>( &mut self, loc: L, data: T, ) -> WorkSheetResult<()>
fn write_string<L: Location>( &mut self, loc: L, data: String, ) -> WorkSheetResult<()>
fn write_rich_string<L: Location>( &mut self, loc: L, data: &RichText, ) -> WorkSheetResult<()>
fn write_number<L: Location>( &mut self, loc: L, data: i32, ) -> WorkSheetResult<()>
fn write_double<L: Location>( &mut self, loc: L, data: f64, ) -> WorkSheetResult<()>
fn write_boolean<L: Location>( &mut self, loc: L, data: bool, ) -> WorkSheetResult<()>
fn write_row<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: &[T], ) -> WorkSheetResult<()>
fn write_row_cells<L: Location, T: CellDisplay + CellValue + Clone>( &mut self, loc: L, cells: &[Cell<T>], ) -> WorkSheetResult<()>
fn write_column<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: &[T], ) -> WorkSheetResult<()>
fn write_column_cells<L: Location, T: CellDisplay + CellValue + Clone>( &mut self, loc: L, cells: &[Cell<T>], ) -> WorkSheetResult<()>
fn write_url<L: Location>(&mut self, loc: L, url: &str) -> WorkSheetResult<()>
fn write_url_text<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, url: &str, data: &str, ) -> WorkSheetResult<()>
fn merge_range<L: LocationRange, T: CellDisplay + CellValue>( &mut self, loc: L, data: T, ) -> WorkSheetResult<()>
fn write_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
fn write_old_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
fn write_array_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
fn write_dynamic_array_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
fn write_with_format<L: Location, T: Default + Clone + CellDisplay + CellValue>( &mut self, loc: L, data: T, format: &Format, ) -> WorkSheetResult<()>
fn write_string_with_format<L: Location>( &mut self, loc: L, data: String, format: &Format, ) -> WorkSheetResult<()>
fn write_number_with_format<L: Location>( &mut self, loc: L, data: i32, format: &Format, ) -> WorkSheetResult<()>
fn write_double_with_format<L: Location>( &mut self, loc: L, data: f64, format: &Format, ) -> WorkSheetResult<()>
fn write_boolean_with_format<L: Location>( &mut self, loc: L, data: bool, format: &Format, ) -> WorkSheetResult<()>
fn write_row_with_format<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: Iter<'_, T>, format: &Format, ) -> WorkSheetResult<()>
fn write_column_with_format<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: Iter<'_, T>, format: &Format, ) -> WorkSheetResult<()>
fn write_url_with_format<L: Location>( &mut self, loc: L, url: &str, format: &Format, ) -> WorkSheetResult<()>
fn write_url_text_with_format<L: Location>( &mut self, loc: L, url: &str, data: &str, format: &Format, ) -> WorkSheetResult<()>
fn write_formula_with_format<L: Location>( &mut self, loc: L, data: &str, format: &Format, ) -> WorkSheetResult<()>
fn write_array_formula_with_format<L: Location>( &mut self, loc: L, data: &str, format: &Format, ) -> WorkSheetResult<()>
fn write_dynamic_array_formula_with_format<L: LocationRange>( &mut self, loc_range: L, data: &str, format: &Format, ) -> WorkSheetResult<()>
fn merge_range_with_format<L: LocationRange, T: CellDisplay + CellValue>( &mut self, loc: L, data: T, format: &Format, ) -> WorkSheetResult<()>
Auto Trait Implementations§
impl Freeze for WorkSheet
impl !RefUnwindSafe for WorkSheet
impl !Send for WorkSheet
impl !Sync for WorkSheet
impl Unpin for WorkSheet
impl !UnwindSafe for WorkSheet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more