Вставка HTML-таблицы в Excel - C#
Формулировка задачи:
Приветствую господа,
У меня есть excel таблица в HTML-формате такого вида:
Если скопировать и вставить ее ручками в excel, то он раскидает значения по столбцам автоматически. Я бы хотел реализовать вставку таблички программно. Если присваивать ячейке текст таблицы, то он весь вставляется в одну ячейку:
Возможно проблему решит использование специальной вставки, но реализовать ее у меня не получилось. У вас есть идеи, как заставить excel распознавать таблицу?
<!--StartFragment--><table cellspacing="0"> <tr> <th colspan="6" align="center">'17'</th> </tr> <tr> <th align="center">Depth(MD)</th> <th align="center">Depth(TVD)</th> <th align="center">Perf_cell</th> <th align="center">Perf</th> <th align="center">Oil</th> <th align="center">Water</th> </tr> <tr> <th nowrap="1">492.12</th> <th nowrap="1">492.12</th> <th nowrap="1">[ 55,147, 8]</th> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> </tr> <tr> <th nowrap="1">493.08</th> <th nowrap="1">493.08</th> <th nowrap="1">[ 55,147, 9]</th> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> </tr> <tr> <th nowrap="1">494.05</th> <th nowrap="1">494.05</th> <th nowrap="1">[ 55,147, 10]</th> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> </tr> <tr> <th nowrap="1">495.02</th> <th nowrap="1">495.02</th> <th nowrap="1">[ 55,147, 11]</th> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> <td nowrap="1" align=right>0</td> </tr> </table>
private void button4_Click(object sender, EventArgs e)
{
excelapp = new Excel.Application();
excelapp.Visible = true;
excelappworkbooks = excelapp.Workbooks;
excelappworkbook = excelapp.Workbooks.Add(Type.Missing);
excelsheets = excelappworkbook.Worksheets;
excelworksheet = (Excel.Worksheet)excelsheets.get_Item(1);
for (int m = 1; m < listBox1.Items.Count; m++)
{
excelcells = (Excel.Range)excelworksheet.Cells[0, m];
excelcells.Value2 = listBox1.Items[m].ToString();
}
}Решение задачи: «Вставка HTML-таблицы в Excel»
textual
Листинг программы
protected void ExportToExcelButton_Click(object sender, EventArgs e)
{
Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
Response.Write(ExportDiv.InnerHtml);
Response.End();
}