"Продвинутое" заполнение шаблона word - C#
Формулировка задачи:
Ну, сразу к делу:
Существует шаблон Word с закладками и поля в программке, из которых нужно брать инфу. Если поле пустое - это пустое значение переписывается в Word:
Пример
1. Текст текст 234 1234 342 3124
2. _ _ _ _ _ _ _(это пустые места, показал для наглядности "_")
3. Текст текст 123 1234 54 1234
Шаблон заполняется вот так:
Для первых 3х элементов написал "продвинутое" заполнение. Т.е. у каждой строки Word есть индекс строки из программы, которой была заполнена строка Word.
И, наконец, вопрос: Есть ли возможность упростить проверку? Т.К. для 29й строки нужно будет писать 29 if(в моем исполнении), а это утомительно...
int[] a; a = new int[30]; for (int i = 0; i < 30; i++) { a[i] = 0; } #region 1 if (Quantity_1.Text != "0" && Weight_1.Text != "0" && cB1.Checked == true) { oDoc.Bookmarks["Type1"].Range.Text = Element_type_dropdown_1.Text; oDoc.Bookmarks["Size1"].Range.Text = Element_dimension_type_dropdown_1.Text; oDoc.Bookmarks["Quantity1"].Range.Text = Quantity_1.Text; oDoc.Bookmarks["WeightPiece1"].Range.Text = Weight_for_piece_1.Text; oDoc.Bookmarks["Weight1"].Range.Text = Weight_1.Text; oDoc.Bookmarks["Rate1"].Range.Text = Rate_1.Text; oDoc.Bookmarks["Area1"].Range.Text = Area_1.Text; a[1] = 1; } #endregion #region 2 if (Quantity_2.Text != "0" && Weight_2.Text != "0" && cB2.Checked == true) { if (a[1] != 1) { oDoc.Bookmarks["Type1"].Range.Text = Element_type_dropdown_2.Text; oDoc.Bookmarks["Size1"].Range.Text = Element_dimension_type_dropdown_2.Text; oDoc.Bookmarks["Quantity1"].Range.Text = Quantity_2.Text; oDoc.Bookmarks["WeightPiece1"].Range.Text = Weight_for_piece_2.Text; oDoc.Bookmarks["Weight1"].Range.Text = Weight_2.Text; oDoc.Bookmarks["Rate1"].Range.Text = Rate_2.Text; oDoc.Bookmarks["Area1"].Range.Text = Area_2.Text; a[1] = 2; } else { oDoc.Bookmarks["Type2"].Range.Text = Element_type_dropdown_2.Text; oDoc.Bookmarks["Size2"].Range.Text = Element_dimension_type_dropdown_2.Text; oDoc.Bookmarks["Quantity2"].Range.Text = Quantity_2.Text; oDoc.Bookmarks["WeightPiece2"].Range.Text = Weight_for_piece_2.Text; oDoc.Bookmarks["Weight2"].Range.Text = Weight_2.Text; oDoc.Bookmarks["Rate2"].Range.Text = Rate_2.Text; oDoc.Bookmarks["Area2"].Range.Text = Area_2.Text; a[2] = 2; } } #endregion #region 3 if (Quantity_3.Text != "0" && Weight_3.Text != "0" && cB3.Checked == true) { if (a[1] != 1 && a[1] != 2) { oDoc.Bookmarks["Type1"].Range.Text = Element_type_dropdown_3.Text; oDoc.Bookmarks["Size1"].Range.Text = Element_dimension_type_dropdown_3.Text; oDoc.Bookmarks["Quantity1"].Range.Text = Quantity_3.Text; oDoc.Bookmarks["WeightPiece1"].Range.Text = Weight_for_piece_3.Text; oDoc.Bookmarks["Weight1"].Range.Text = Weight_3.Text; oDoc.Bookmarks["Rate1"].Range.Text = Rate_3.Text; oDoc.Bookmarks["Area1"].Range.Text = Area_3.Text; } else if (a[2] != 2) { oDoc.Bookmarks["Type2"].Range.Text = Element_type_dropdown_3.Text; oDoc.Bookmarks["Size2"].Range.Text = Element_dimension_type_dropdown_3.Text; oDoc.Bookmarks["Quantity2"].Range.Text = Quantity_3.Text; oDoc.Bookmarks["WeightPiece2"].Range.Text = Weight_for_piece_3.Text; oDoc.Bookmarks["Weight2"].Range.Text = Weight_3.Text; oDoc.Bookmarks["Rate2"].Range.Text = Rate_3.Text; oDoc.Bookmarks["Area2"].Range.Text = Area_3.Text; } else { oDoc.Bookmarks["Type3"].Range.Text = Element_type_dropdown_3.Text; oDoc.Bookmarks["Size3"].Range.Text = Element_dimension_type_dropdown_3.Text; oDoc.Bookmarks["Quantity3"].Range.Text = Quantity_3.Text; oDoc.Bookmarks["WeightPiece3"].Range.Text = Weight_for_piece_3.Text; oDoc.Bookmarks["Weight3"].Range.Text = Weight_3.Text; oDoc.Bookmarks["Rate3"].Range.Text = Rate_3.Text; oDoc.Bookmarks["Area3"].Range.Text = Area_3.Text; } } #endregion if (Quantity_4.Text != "0" && Weight_4.Text != "0" && cB4.Checked == true) { oDoc.Bookmarks["Type4"].Range.Text = Element_type_dropdown_4.Text; oDoc.Bookmarks["Size4"].Range.Text = Element_dimension_type_dropdown_4.Text; oDoc.Bookmarks["Quantity4"].Range.Text = Quantity_4.Text; oDoc.Bookmarks["WeightPiece4"].Range.Text = Weight_for_piece_4.Text; oDoc.Bookmarks["Weight4"].Range.Text = Weight_4.Text; oDoc.Bookmarks["Rate4"].Range.Text = Rate_4.Text; oDoc.Bookmarks["Area4"].Range.Text = Area_4.Text; } if (Quantity_5.Text != "0" && Weight_5.Text != "0" && cB5.Checked == true) { oDoc.Bookmarks["Type5"].Range.Text = Element_type_dropdown_5.Text; oDoc.Bookmarks["Size5"].Range.Text = Element_dimension_type_dropdown_5.Text; oDoc.Bookmarks["Quantity5"].Range.Text = Quantity_5.Text; oDoc.Bookmarks["WeightPiece5"].Range.Text = Weight_for_piece_5.Text; oDoc.Bookmarks["Weight5"].Range.Text = Weight_5.Text; oDoc.Bookmarks["Rate5"].Range.Text = Rate_5.Text; oDoc.Bookmarks["Area5"].Range.Text = Area_5.Text; } if (Quantity_6.Text != "0" && Weight_6.Text != "0" && cB6.Checked == true) { oDoc.Bookmarks["Type6"].Range.Text = Element_type_dropdown_6.Text; oDoc.Bookmarks["Size6"].Range.Text = Element_dimension_type_dropdown_6.Text; oDoc.Bookmarks["Quantity6"].Range.Text = Quantity_6.Text; oDoc.Bookmarks["WeightPiece6"].Range.Text = Weight_for_piece_6.Text; oDoc.Bookmarks["Weight6"].Range.Text = Weight_6.Text; oDoc.Bookmarks["Rate6"].Range.Text = Rate_6.Text; oDoc.Bookmarks["Area6"].Range.Text = Area_6.Text; } if (Quantity_7.Text != "0" && Weight_7.Text != "0" && cB7.Checked == true) { oDoc.Bookmarks["Type7"].Range.Text = Element_type_dropdown_7.Text; oDoc.Bookmarks["Size7"].Range.Text = Element_dimension_type_dropdown_7.Text; oDoc.Bookmarks["Quantity7"].Range.Text = Quantity_7.Text; oDoc.Bookmarks["WeightPiece7"].Range.Text = Weight_for_piece_7.Text; oDoc.Bookmarks["Weight7"].Range.Text = Weight_7.Text; oDoc.Bookmarks["Rate7"].Range.Text = Rate_7.Text; oDoc.Bookmarks["Area7"].Range.Text = Area_7.Text; } if (Quantity_8.Text != "0" && Weight_8.Text != "0" && cB8.Checked == true) { oDoc.Bookmarks["Type8"].Range.Text = Element_type_dropdown_8.Text; oDoc.Bookmarks["Size8"].Range.Text = Element_dimension_type_dropdown_8.Text; oDoc.Bookmarks["Quantity8"].Range.Text = Quantity_8.Text; oDoc.Bookmarks["WeightPiece8"].Range.Text = Weight_for_piece_8.Text; oDoc.Bookmarks["Weight8"].Range.Text = Weight_8.Text; oDoc.Bookmarks["Rate8"].Range.Text = Rate_8.Text; oDoc.Bookmarks["Area8"].Range.Text = Area_8.Text; } if (Quantity_9.Text != "0" && Weight_9.Text != "0" && cB9.Checked == true) { oDoc.Bookmarks["Type9"].Range.Text = Element_type_dropdown_9.Text; oDoc.Bookmarks["Size9"].Range.Text = Element_dimension_type_dropdown_9.Text; oDoc.Bookmarks["Quantity9"].Range.Text = Quantity_9.Text; oDoc.Bookmarks["WeightPiece9"].Range.Text = Weight_for_piece_9.Text; oDoc.Bookmarks["Weight9"].Range.Text = Weight_9.Text; oDoc.Bookmarks["Rate9"].Range.Text = Rate_9.Text; oDoc.Bookmarks["Area9"].Range.Text = Area_9.Text; } if (Quantity_10.Text != "0" && Weight_10.Text != "0" && cB10.Checked == true) { oDoc.Bookmarks["Type10"].Range.Text = Element_type_dropdown_10.Text; oDoc.Bookmarks["Size10"].Range.Text = Element_dimension_type_dropdown_10.Text; oDoc.Bookmarks["Quantity10"].Range.Text = Quantity_10.Text; oDoc.Bookmarks["WeightPiece10"].Range.Text = Weight_for_piece_10.Text; oDoc.Bookmarks["Weight10"].Range.Text = Weight_10.Text; oDoc.Bookmarks["Rate10"].Range.Text = Rate_10.Text; oDoc.Bookmarks["Area10"].Range.Text = Area_10.Text; } if (Quantity_11.Text != "0" && Weight_11.Text != "0" && cB11.Checked == true) { oDoc.Bookmarks["Type11"].Range.Text = Element_type_dropdown_11.Text; oDoc.Bookmarks["Size11"].Range.Text = Element_dimension_type_dropdown_11.Text; oDoc.Bookmarks["Quantity11"].Range.Text = Quantity_11.Text; oDoc.Bookmarks["WeightPiece11"].Range.Text = Weight_for_piece_11.Text; oDoc.Bookmarks["Weight11"].Range.Text = Weight_11.Text; oDoc.Bookmarks["Rate11"].Range.Text = Rate_11.Text; oDoc.Bookmarks["Area11"].Range.Text = Area_11.Text; } if (Quantity_12.Text != "0" && Weight_12.Text != "0" && cB12.Checked == true) { oDoc.Bookmarks["Type12"].Range.Text = Element_type_dropdown_12.Text; oDoc.Bookmarks["Size12"].Range.Text = Element_dimension_type_dropdown_12.Text; oDoc.Bookmarks["Quantity12"].Range.Text = Quantity_12.Text; oDoc.Bookmarks["WeightPiece12"].Range.Text = Weight_for_piece_12.Text; oDoc.Bookmarks["Weight12"].Range.Text = Weight_12.Text; oDoc.Bookmarks["Rate12"].Range.Text = Rate_12.Text; oDoc.Bookmarks["Area12"].Range.Text = Area_12.Text; } if (Quantity_13.Text != "0" && Weight_13.Text != "0" && cB13.Checked == true) { oDoc.Bookmarks["Type13"].Range.Text = Element_type_dropdown_13.Text; oDoc.Bookmarks["Size13"].Range.Text = Element_dimension_type_dropdown_13.Text; oDoc.Bookmarks["Quantity13"].Range.Text = Quantity_13.Text; oDoc.Bookmarks["WeightPiece13"].Range.Text = Weight_for_piece_13.Text; oDoc.Bookmarks["Weight13"].Range.Text = Weight_13.Text; oDoc.Bookmarks["Rate13"].Range.Text = Rate_13.Text; oDoc.Bookmarks["Area13"].Range.Text = Area_13.Text; } if (Quantity_14.Text != "0" && Weight_14.Text != "0" && cB14.Checked == true) { oDoc.Bookmarks["Type14"].Range.Text = Element_type_dropdown_14.Text; oDoc.Bookmarks["Size14"].Range.Text = Element_dimension_type_dropdown_14.Text; oDoc.Bookmarks["Quantity14"].Range.Text = Quantity_14.Text; oDoc.Bookmarks["WeightPiece14"].Range.Text = Weight_for_piece_14.Text; oDoc.Bookmarks["Weight14"].Range.Text = Weight_14.Text; oDoc.Bookmarks["Rate14"].Range.Text = Rate_14.Text; oDoc.Bookmarks["Area14"].Range.Text = Area_14.Text; } if (Quantity_15.Text != "0" && Weight_15.Text != "0" && cB15.Checked == true) { oDoc.Bookmarks["Type15"].Range.Text = Element_type_dropdown_15.Text; oDoc.Bookmarks["Size15"].Range.Text = Element_dimension_type_dropdown_15.Text; oDoc.Bookmarks["Quantity15"].Range.Text = Quantity_15.Text; oDoc.Bookmarks["WeightPiece15"].Range.Text = Weight_for_piece_15.Text; oDoc.Bookmarks["Weight15"].Range.Text = Weight_15.Text; oDoc.Bookmarks["Rate15"].Range.Text = Rate_15.Text; oDoc.Bookmarks["Area15"].Range.Text = Area_15.Text; } if (Quantity_16.Text != "0" && Weight_16.Text != "0" && cB16.Checked == true) { oDoc.Bookmarks["Type16"].Range.Text = Element_type_dropdown_16.Text; oDoc.Bookmarks["Size16"].Range.Text = Element_dimension_type_dropdown_16.Text; oDoc.Bookmarks["Quantity16"].Range.Text = Quantity_16.Text; oDoc.Bookmarks["WeightPiece16"].Range.Text = Weight_for_piece_16.Text; oDoc.Bookmarks["Weight16"].Range.Text = Weight_16.Text; oDoc.Bookmarks["Rate16"].Range.Text = Rate_16.Text; oDoc.Bookmarks["Area16"].Range.Text = Area_16.Text; } if (Quantity_17.Text != "0" && Weight_17.Text != "0" && cB17.Checked == true) { oDoc.Bookmarks["Type17"].Range.Text = Element_type_dropdown_17.Text; oDoc.Bookmarks["Size17"].Range.Text = Element_dimension_type_dropdown_17.Text; oDoc.Bookmarks["Quantity17"].Range.Text = Quantity_17.Text; oDoc.Bookmarks["WeightPiece17"].Range.Text = Weight_for_piece_17.Text; oDoc.Bookmarks["Weight17"].Range.Text = Weight_17.Text; oDoc.Bookmarks["Rate17"].Range.Text = Rate_17.Text; oDoc.Bookmarks["Area17"].Range.Text = Area_17.Text; } if (Quantity_18.Text != "0" && Weight_18.Text != "0" && cB18.Checked == true) { oDoc.Bookmarks["Type18"].Range.Text = Element_type_dropdown_18.Text; oDoc.Bookmarks["Size18"].Range.Text = Element_dimension_type_dropdown_18.Text; oDoc.Bookmarks["Quantity18"].Range.Text = Quantity_18.Text; oDoc.Bookmarks["WeightPiece18"].Range.Text = Weight_for_piece_18.Text; oDoc.Bookmarks["Weight18"].Range.Text = Weight_18.Text; oDoc.Bookmarks["Rate18"].Range.Text = Rate_18.Text; oDoc.Bookmarks["Area18"].Range.Text = Area_18.Text; } if (Quantity_19.Text != "0" && Weight_19.Text != "0" && cB19.Checked == true) { oDoc.Bookmarks["Type19"].Range.Text = Element_type_dropdown_19.Text; oDoc.Bookmarks["Size19"].Range.Text = Element_dimension_type_dropdown_19.Text; oDoc.Bookmarks["Quantity19"].Range.Text = Quantity_19.Text; oDoc.Bookmarks["WeightPiece19"].Range.Text = Weight_for_piece_19.Text; oDoc.Bookmarks["Weight19"].Range.Text = Weight_19.Text; oDoc.Bookmarks["Rate19"].Range.Text = Rate_19.Text; oDoc.Bookmarks["Area19"].Range.Text = Area_19.Text; } if (Quantity_20.Text != "0" && Weight_20.Text != "0" && cB20.Checked == true) { oDoc.Bookmarks["Type20"].Range.Text = Element_type_dropdown_20.Text; oDoc.Bookmarks["Size20"].Range.Text = Element_dimension_type_dropdown_20.Text; oDoc.Bookmarks["Quantity20"].Range.Text = Quantity_20.Text; oDoc.Bookmarks["WeightPiece20"].Range.Text = Weight_for_piece_20.Text; oDoc.Bookmarks["Weight20"].Range.Text = Weight_20.Text; oDoc.Bookmarks["Rate20"].Range.Text = Rate_20.Text; oDoc.Bookmarks["Area20"].Range.Text = Area_20.Text; } if (Quantity_21.Text != "0" && Weight_21.Text != "0" && cB21.Checked == true) { oDoc.Bookmarks["Type21"].Range.Text = Element_type_dropdown_21.Text; oDoc.Bookmarks["Size21"].Range.Text = Element_dimension_type_dropdown_21.Text; oDoc.Bookmarks["Quantity21"].Range.Text = Quantity_21.Text; oDoc.Bookmarks["WeightPiece21"].Range.Text = Weight_for_piece_21.Text; oDoc.Bookmarks["Weight21"].Range.Text = Weight_21.Text; oDoc.Bookmarks["Rate21"].Range.Text = Rate_21.Text; oDoc.Bookmarks["Area21"].Range.Text = Area_21.Text; } if (Quantity_22.Text != "0" && Weight_22.Text != "0" && cB22.Checked == true) { oDoc.Bookmarks["Type22"].Range.Text = Element_type_dropdown_22.Text; oDoc.Bookmarks["Size22"].Range.Text = Element_dimension_type_dropdown_22.Text; oDoc.Bookmarks["Quantity22"].Range.Text = Quantity_22.Text; oDoc.Bookmarks["WeightPiece22"].Range.Text = Weight_for_piece_22.Text; oDoc.Bookmarks["Weight22"].Range.Text = Weight_22.Text; oDoc.Bookmarks["Rate22"].Range.Text = Rate_22.Text; oDoc.Bookmarks["Area22"].Range.Text = Area_22.Text; } if (Quantity_23.Text != "0" && Weight_23.Text != "0" && cB23.Checked == true) { oDoc.Bookmarks["Type23"].Range.Text = Element_type_dropdown_23.Text; oDoc.Bookmarks["Size23"].Range.Text = Element_dimension_type_dropdown_23.Text; oDoc.Bookmarks["Quantity23"].Range.Text = Quantity_23.Text; oDoc.Bookmarks["WeightPiece23"].Range.Text = Weight_for_piece_23.Text; oDoc.Bookmarks["Weight23"].Range.Text = Weight_23.Text; oDoc.Bookmarks["Rate23"].Range.Text = Rate_23.Text; oDoc.Bookmarks["Area23"].Range.Text = Area_23.Text; } if (Quantity_24.Text != "0" && Weight_24.Text != "0" && cB24.Checked == true) { oDoc.Bookmarks["Type24"].Range.Text = Element_type_dropdown_24.Text; oDoc.Bookmarks["Size24"].Range.Text = Element_dimension_type_dropdown_24.Text; oDoc.Bookmarks["Quantity24"].Range.Text = Quantity_24.Text; oDoc.Bookmarks["WeightPiece24"].Range.Text = Weight_for_piece_24.Text; oDoc.Bookmarks["Weight24"].Range.Text = Weight_24.Text; oDoc.Bookmarks["Rate24"].Range.Text = Rate_24.Text; oDoc.Bookmarks["Area24"].Range.Text = Area_24.Text; } if (Quantity_25.Text != "0" && Weight_25.Text != "0" && cB25.Checked == true) { oDoc.Bookmarks["Type25"].Range.Text = Element_type_dropdown_25.Text; oDoc.Bookmarks["Size25"].Range.Text = Element_dimension_type_dropdown_25.Text; oDoc.Bookmarks["Quantity25"].Range.Text = Quantity_25.Text; oDoc.Bookmarks["WeightPiece25"].Range.Text = Weight_for_piece_25.Text; oDoc.Bookmarks["Weight25"].Range.Text = Weight_25.Text; oDoc.Bookmarks["Rate25"].Range.Text = Rate_25.Text; oDoc.Bookmarks["Area25"].Range.Text = Area_25.Text; } if (Quantity_26.Text != "0" && Weight_26.Text != "0" && cB26.Checked == true) { oDoc.Bookmarks["Type26"].Range.Text = Element_type_dropdown_26.Text; oDoc.Bookmarks["Size26"].Range.Text = Element_dimension_type_dropdown_26.Text; oDoc.Bookmarks["Quantity26"].Range.Text = Quantity_26.Text; oDoc.Bookmarks["WeightPiece26"].Range.Text = Weight_for_piece_26.Text; oDoc.Bookmarks["Weight26"].Range.Text = Weight_26.Text; oDoc.Bookmarks["Rate26"].Range.Text = Rate_26.Text; oDoc.Bookmarks["Area26"].Range.Text = Area_26.Text; } if (Quantity_27.Text != "0" && Weight_27.Text != "0" && cB27.Checked == true) { oDoc.Bookmarks["Type27"].Range.Text = Element_type_dropdown_27.Text; oDoc.Bookmarks["Size27"].Range.Text = Element_dimension_type_dropdown_27.Text; oDoc.Bookmarks["Quantity27"].Range.Text = Quantity_27.Text; oDoc.Bookmarks["WeightPiece27"].Range.Text = Weight_for_piece_27.Text; oDoc.Bookmarks["Weight27"].Range.Text = Weight_27.Text; oDoc.Bookmarks["Rate27"].Range.Text = Rate_27.Text; oDoc.Bookmarks["Area27"].Range.Text = Area_27.Text; } if (Quantity_28.Text != "0" && Weight_28.Text != "0" && cB28.Checked == true) { oDoc.Bookmarks["Type28"].Range.Text = Element_type_dropdown_28.Text; oDoc.Bookmarks["Size28"].Range.Text = Element_dimension_type_dropdown_28.Text; oDoc.Bookmarks["Quantity28"].Range.Text = Quantity_28.Text; oDoc.Bookmarks["WeightPiece28"].Range.Text = Weight_for_piece_28.Text; oDoc.Bookmarks["Weight28"].Range.Text = Weight_28.Text; oDoc.Bookmarks["Rate28"].Range.Text = Rate_28.Text; oDoc.Bookmarks["Area28"].Range.Text = Area_28.Text; } if (Quantity_29.Text != "0" && Weight_29.Text != "0" && cB29.Checked == true) { oDoc.Bookmarks["Type29"].Range.Text = Element_type_dropdown_29.Text; oDoc.Bookmarks["Size29"].Range.Text = Element_dimension_type_dropdown_29.Text; oDoc.Bookmarks["Quantity29"].Range.Text = Quantity_29.Text; oDoc.Bookmarks["WeightPiece29"].Range.Text = Weight_for_piece_29.Text; oDoc.Bookmarks["Weight29"].Range.Text = Weight_29.Text; oDoc.Bookmarks["Rate29"].Range.Text = Rate_29.Text; oDoc.Bookmarks["Area29"].Range.Text = Area_29.Text; } if (Quantity_30.Text != "0" && Weight_30.Text != "0" && cB30.Checked == true) { oDoc.Bookmarks["Type30"].Range.Text = Element_type_dropdown_30.Text; oDoc.Bookmarks["Size30"].Range.Text = Element_dimension_type_dropdown_30.Text; oDoc.Bookmarks["Quantity30"].Range.Text = Quantity_30.Text; oDoc.Bookmarks["WeightPiece30"].Range.Text = Weight_for_piece_30.Text; oDoc.Bookmarks["Weight30"].Range.Text = Weight_30.Text; oDoc.Bookmarks["Rate30"].Range.Text = Rate_30.Text; oDoc.Bookmarks["Area30"].Range.Text = Area_30.Text; }
Решение задачи: «"Продвинутое" заполнение шаблона word»
textual
Листинг программы
Word.Table table = wordDoc.Tables[2]; //для каждой строчки с данными for (int i=0; i<xml.Tables[4].Rows.Count;i++) { table.Rows.Add(table.Rows[i+2]); toolStripStatusLabel1.Text = "Записано: " + i; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д