如题, 请各位帮帮忙.

解决方案 »

  1.   

    说个思路,遍历每一个Sheet转换为DataTable,完后把得到的所有DataTable再重新添加的同一个Sheet中..可供参考
      

  2.   

    我用了一个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的进程,这个你自己搞搞看看,