Как записать массив string в одну ячейку Excel - C#
Формулировка задачи:
Добрый день, друзья! Возникла небольшая проблемка с записей массива string[], который получается из заполнения MultiLine TextBox, в одну ячейку Excel. А так же значения из ComboBox не записываются в Excel. Помогите разобраться, пожалуйста, в чем ошибка? Сразу же прошу прощения за **вно-код =(
Листинг программы
- public void AddTask_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("Вы действительно хотите добавить новую задачу?", "Проверка", MessageBoxButtons.YesNo) == DialogResult.No)
- return;
- InitializeComponent();
- int iLastRow, iLastCol;
- Excel.Application xlApp = new Excel.Application(); //открыть эксель
- System.Diagnostics.Process excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL").Last();
- Excel.Workbook xlWB = xlApp.Workbooks.Open(@"\\Reestr\Reestr.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //открыть файл
- Excel.Worksheet xlSht = (Excel.Worksheet)xlWB.Sheets[1]; //получить 1 лист
- iLastRow = xlSht.Cells[xlSht.Rows.Count, "A"].End[Excel.XlDirection.xlUp].Row; //последняя заполненная строка в столбце А
- iLastCol = xlSht.Cells[1, xlSht.Columns.Count].End[Excel.XlDirection.xlToLeft].Column; //последний заполненный столбец в 1-й строке
- // Нахождение максимального значения
- int maxID = 0;
- for(int i =1;i<iLastRow+1;i++ )
- {
- int temp = 0;
- Int32.TryParse(xlSht.Cells[i, 1].Text, out temp);
- if (temp > maxID)
- maxID = temp;
- }
- int maxPrioritet = 0;
- for (int i = 1; i < iLastRow + 1; i++)
- {
- int temp = 0;
- Int32.TryParse(xlSht.Cells[i, 1].Text, out temp);
- if (temp > maxPrioritet)
- maxPrioritet = temp;
- }
- // Запись в ячейки экселя
- xlSht.Cells[iLastRow + 1, 1] = maxID + 1;
- xlSht.Cells[iLastRow + 1, 2] = textBox1.Text; //ВОТ ТУТ ОШИБКА!!
- xlSht.Cells[iLastRow + 1, 3] = textBox2.Text; //ВОТ ТУТ ОШИБКА!!
- xlSht.Cells[iLastRow + 1, 4] = comboBox1.Text; //ВОТ ТУТ ОШИБКА!!
- xlSht.Cells[iLastRow + 1, 5] = Environment.UserName;
- xlSht.Cells[iLastRow + 1, 6] = "Добавлена";
- xlSht.Cells[iLastRow + 1, 9] = dateTimePicker1.Value.Date;
- xlSht.Cells[iLastRow + 1, 10] = comboBox2.Text; //ВОТ ТУТ ОШИБКА!!
- xlSht.Cells[iLastRow + 1, 11] = 0;
- xlSht.Cells[iLastRow + 1, 12] = maxPrioritet+1;
- xlSht.Cells[iLastRow + 1, 13] = comboBox3.Text; //ВОТ ТУТ ОШИБКА!!
- xlSht.Cells[iLastRow + 1, 14] = DateTime.Today.Date;
- xlSht.Cells[iLastRow + 1, 15] = textBox3.Text; //ВОТ ТУТ ОШИБКА!!
- xlWB.Close(true); //закрыть и сохранить книгу
- xlApp.Quit();
- excelProc.Kill();
- GC.Collect();
- MessageBox.Show("Задача успешно добавлена!");
- }
Решение задачи: «Как записать массив string в одну ячейку Excel»
textual
Листинг программы
- xlSht.Cells[iLastRow + 1].Select();
- xlApp.Visible = true;
- ;// Сюда breakpoint. Поводить мышкой по всем textBox.Text, переменным и посмотреть что сейчас на листе в Excel
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д