用C#如何将excel中多个工作表的内容合并到一个工作表下面 如题, 请各位帮帮忙. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 说个思路,遍历每一个Sheet转换为DataTable,完后把得到的所有DataTable再重新添加的同一个Sheet中..可供参考 我用了一个excel文件一次读出其中所有的数据,然后再写到另外一个文件里面~~所有代码贴出来,你看看吧using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Data.OleDb;namespace WindowsApplication1{ public partial class Form2 : Form { DataTable dt1; public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dt1 = this.getDataTable("e:\\text1.xls"); } private DataTable getDataTable(string strpath) { DataTable dt; try { string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strpath + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$],[Sheet2$],[Sheet3$]", strConn); dt = new DataTable(); myCommand.Fill(dt); return dt; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } return null; } public void DataTabletoExcel(DataTable tmpDataTable, string strFileName) { if (tmpDataTable == null) { return; } int rowNum = tmpDataTable.Rows.Count; int columnNum = tmpDataTable.Columns.Count; int rowIndex = 1; int columnIndex = 0; Excel.Application xlApp = new Excel.ApplicationClass(); xlApp.DefaultFilePath = ""; xlApp.DisplayAlerts = true; xlApp.SheetsInNewWorkbook = 1; Excel.Workbook xlBook = xlApp.Workbooks.Add(true); //将DataTable的列名导入Excel表第一行 foreach (DataColumn dc in tmpDataTable.Columns) { columnIndex++; xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName; } //将DataTable中的数据导入Excel中 for (int i = 0; i < rowNum; i++) { rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++) { columnIndex++; xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString(); } } xlBook.SaveCopyAs(strFileName); } private void button2_Click(object sender, EventArgs e) { DataTabletoExcel(dt1, "e:\\test.xls"); } }}这是第一个工作区这是第二个工作区这是第三个工作区这是合成的excel工作表基本上可以,但是有一个问题要注意,最后的话有可能要杀掉excel的进程,这个你自己搞搞看看, 新手问题,winform程序按F5运行得好好的,从bin里面拷出来就经常出错 如何将dataview的数据 加上日期 写入一张新的数据库表中(winform) c# 怎样实现窗体加载完之后再加载datagrid的数据? 怎样获得datagridview列标题小箭头的方向? 关于SQL连接的问题 新手求教 C#的编程环境 帮忙看看错误提示! 问两个关于日期的小问题,谢谢 为什么在 Access 里这个 SQL 子查询查不到结果呢? 请问:sum +=++i怎么计算? 高手帮忙 C#怎么样调用ORACLE中的函数
所有代码贴出来,你看看吧
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;namespace WindowsApplication1
{
public partial class Form2 : Form
{
DataTable dt1;
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
dt1 = this.getDataTable("e:\\text1.xls");
} private DataTable getDataTable(string strpath)
{
DataTable dt;
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strpath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$],[Sheet2$],[Sheet3$]", strConn);
dt = new DataTable();
myCommand.Fill(dt);
return dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
} return null;
} public void DataTabletoExcel(DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
{
return;
}
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0; Excel.Application xlApp = new Excel.ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true); //将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
} //将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
xlBook.SaveCopyAs(strFileName);
} private void button2_Click(object sender, EventArgs e)
{
DataTabletoExcel(dt1, "e:\\test.xls");
}
}
}这是第一个工作区
这是第二个工作区
这是第三个工作区这是合成的excel工作表
基本上可以,但是有一个问题要注意,最后的话有可能要杀掉excel的进程,这个你自己搞搞看看,