How to copy excel columns from one file to another?

泄露秘密 提交于 2020-01-05 17:09:24

问题


I using excel addin application, to create function which can copy columns from one excel file to another. Here is the code so far but when I render the application, however the program outputs a blank book.xls file.

private void button1_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        Excel.Workbook xlWorkBook;
        Excel.Workbook xlWorkBook2;

        Excel.Worksheet xlWorkSheet;
        Excel.Worksheet xlWsheet2;

        Excel.Range xlSourceRange;
        Excel.Range xlSourceRange1;

        Excel.Range xlDestRange;
        Excel.Range xlDestRange1;

        xlWorkBook = xlApp.Workbooks.Open("C:/../../../../../../../Test.xls");


        xlWorkBook2 = xlApp.Workbooks.Open("C:/../../../../../../../Book1.xls");

        //~~> Display Excel
        xlApp.Visible = true;

        //~~> Set the source worksheet
        xlWorkSheet = xlWorkBook.Sheets["Sheet1"];
        //~~> Set the destination worksheet
        xlWsheet2 = xlWorkBook2.Sheets["Sheet1"];

        //~~> Set the source range
        xlSourceRange = xlWorkSheet.Range["E15"].EntireColumn;
        xlSourceRange1 = xlWorkSheet.Range["D15"].EntireColumn;

        //~~> Set the destination range
        xlDestRange = xlWsheet2.Range["A2"];
        xlDestRange1 = xlWsheet2.Range["B2"];


        xlSourceRange.Copy(Type.Missing);

        xlDestRange.PasteSpecial(Excel.XlPasteType.xlPasteAll,
Excel.XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);


        xlSourceRange1.Copy(Type.Missing);

        xlDestRange1.PasteSpecial(Excel.XlPasteType.xlPasteAll,
Excel.XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);

    }

I am little unsure how I to go about tracing for errors, as I am currently an novice user - working with excel libraries. Any further assistance would be most appreciated.
Thanks


回答1:


friend please use below code,use your column name instance of this query "select columnname from sheetName"

static void Main(string[] args)
    {
        string sourceFileName = @"C:\Test.xls";
        DataTable dataTable = loadSingleSheet(sourceFileName, "Employee");
        dataTable.Columns.Add("COLUMN_A");
        dataTable.Columns.Remove("COLUMN_B");
    }

    private static OleDbConnection ReturnConnection(string fileName)
    {
        return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;Extended Properties=\"Excel 8.0;\"");
    }

    private static DataTable LoadSingleSheet(string fileName, string sheetName)
    {
        DataTable sheetData = new DataTable();
        using (OleDbConnection conn = ReturnConnection(fileName))
        {
            conn.Open();

            OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select columnname from [" + sheetName + "]", conn);
            sheetAdapter.Fill(sheetData);
        }
        return sheetData;
    }




private static void UpdateSingleSheet(string fileName, string sheetName, DataTable dataTable)
    {
        using (OleDbConnection conn = ReturnConnection(fileName))
        {
            conn.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand("SELECT columnname FROM [" + sheetName + "]", conn);

            OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

            adapter.UpdateCommand = builder.GetUpdateCommand();
            adapter.Update(dataTable);
        }
    }

I call it this way:

string destinationFileName = @"C:\TestNew.xls";
UpdateSingleSheet(destinationFileName, "Employee", dataTable);

for more information find this site. http://www.codedisqus.com/CyVjkWkUeg/copying-data-from-one-excel-file-to-another-using-c-adonet-ms-jet-returns-error-about-key-column.html



来源:https://stackoverflow.com/questions/30894268/how-to-copy-excel-columns-from-one-file-to-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!