C# ,winform把datatable导出到excel并且在列标题上面增加一行。

其实也就是有格式的导出到Excel

连接字符串里面有相关的关键字选项,如下(07版) Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls; Extended Properties="Excel 8.0;HDR=YES"; HDR=YES就代表第一行是标题。
温馨提示:内容为网友见解,仅供参考
第1个回答  2018-03-28
dt.Columns.Add("col1", typeof(string)); dt.Columns.Add("col2", typeof(string)); dt.Columns.Add("col3", typeof(string)); dt.Rows.Add(new object[] { col1, col2, col3 }); 这个连列也建了,按需修改吧
第2个回答  2018-03-28
dt.Columns.Add("col1", typeof(string)); dt.Columns.Add("col2", typeof(string)); dt.Columns.Add("col3", typeof(string)); dt.Rows.Add(new object[] { col1, col2, col3 }); 这个连列也建了,按需修改吧
第3个回答  推荐于2016-05-26
核心部分的代码

private void 保存为excelToolStripMenuItem_Click(object sender, EventArgs e)
{
this.saveexcel();
}
public async void saveexcel()
{
if (File.Exists(@"C:\Users\Administrator\Documents\111.xls"))//当路径下存在文件时,删除,保证无异常
{
File.Delete(@"C:\Users\Administrator\Documents\111.xls");
}

TimeSpan t1 = new TimeSpan(DateTime.Now.Ticks);//保留实时时间,后期要计算时间差

//——————————————————————————————核心代码部分
this.toolStripProgressBar1.Maximum = this.dataGridView1.Rows.Count-1;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelworkbook = excel.Application.Workbooks.Add(true);
excel.Visible = false;//是否显示excel文本

try
{
//异步执行,保证前台窗口程序无卡顿
await Task.Run(() =>
{
for (int i = 0; i < this.dataGridView1.Columns.Count; i++)//复制表格的列头
{
excel.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;//excel对象的行列索引是从1开始的
//datagridview的行列索引是从0开始的
}
for (int i = 0; i < this.dataGridView1.Rows.Count-1; i++)//减去列头的一行
{
for (int j = 0; j < this.dataGridView1.Columns.Count; j++)
{
if(this.dataGridView1.Rows[i + 1].Cells[j].Value==null)
{
excel.Cells[i + 2, j + 1] ="' ";//当表格的单元格为空时,保留行并跳过
break;
}
else if (this.dataGridView1.Rows[i + 1].Cells[j].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + this.dataGridView1.Rows[i + 1].Cells[j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = this.dataGridView1.Rows[i + 1].Cells[j].Value.ToString();
}
}
this.toolStripProgressBar1.Value++;//进度条前进
}
});
}
catch (Exception ex)
{
}
finally
{
//保存xls表格
excelworkbook.SaveAs("111.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//释放资源
if(excelworkbook!=null)
excelworkbook.Close(Missing.Value, Missing.Value, Missing.Value);
if (excel != null)
{
excel.Workbooks.Close();
excel.Quit();
}
//——————————————————————后续代码非核心代码

//计算时间差
TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks);
TimeSpan ts = t1.Subtract(t2).Duration();
string hours = ts.Hours.ToString(), minutes = ts.Minutes.ToString(), seconds = ts.Seconds.ToString();
if (ts.Hours < 10)
{
hours = "0" + ts.Hours.ToString();
}
if (ts.Minutes < 10)
{
minutes = "0" + ts.Minutes.ToString();
}
if (ts.Seconds < 10)
{
seconds = "0" + ts.Seconds.ToString();
}
if( MessageBox.Show("花费时间\n" + hours + ":" + minutes + ":" + seconds, "完成")==DialogResult.OK)
{
this.toolStripProgressBar1.Value = 0;
}
}

}

早期我自己有做过相关的示例,主要看核心代码部分,使用微软的Microsoft.Office.Interop.Excel.dll库就可以了,很容易实现;
主要是两个要点
①Microsoft.Office.Interop.Excel.Application对象excel的行列索引是从1开始,和datagridview从0开始索引区别开来,
②如果datagirdview单元的值是string类型时,后面加个 ‘

开发时需要的Microsoft.Office.Interop.Excel.dll组件http://pan.baidu.com/s/1jGrPHrS本回答被提问者和网友采纳
第4个回答  2018-03-28
private void AddDataToExcel(System.Data.DataTable dt, string filename) { try { //write Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Ex...
相似回答