------解决方案-------------------- 就是生成xml格式的excel文件,直接写成文件就可以了,可以生成多个Sheet。 你还可以使用Excel.Application来实现
------解决方案-------------------- private void btn_Dc_Click(object sender, EventArgs e) { if (lbl_SAMPLE_ID.Text == "lbl_SAMPLE_IDNone") { MsgBox.alert(ReturnTck.getTck("tck_tjbb_01")); return; } g_strSql = @"select a.DataCell_ID,ParentCell_ID,APPLI_OLDNAME,APPLI_NAME,a.APPLI_TYPE,Cell_No,Config_Start, Start_Bit,Config_Over,Over_Bit,Config_AValue,Config_BValue,b.APPLI_TYPE,MODE_MIN,MODE_MAX from Basic_dataCell a left join Sys_Config b on a.DataCell_ID=b.DataCell_ID where a.Sample_ID='" + lbl_SAMPLE_ID.Text + "' order by ParentCell_ID,Cell_No"; DataTable m_dtExcel = MySqlHelper.ExecuteDataTable(g_strSql); if (m_dtExcel.Rows.Count > 0) { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(true); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; Microsoft.Office.Interop.Excel.Range range = null; excel.Visible = true;
long rowRead = 0;
for (int i = 0; i < m_dtExcel.Columns.Count; i++) { worksheet.Cells[1, i + 1] = m_dtExcel.Columns[i].ColumnName; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; } //写入数值 for (int r = 0; r < m_dtExcel.Rows.Count; r++) { for (int i = 0; i < m_dtExcel.Columns.Count; i++) { worksheet.Cells[r + 2, i + 1] = "'" + m_dtExcel.Rows[r][i]; } rowRead++; }