0

我需要将特定工作表从源 excel 文件复制到使用 c# 新创建的 excel 工作表中,并且在你的帮助下我能够成功。但是,还有一个问题我找不到像这样的解决方案。

当所选的表被复制到程序创建的新Excel文件时,链接从单元格到源文件中的链接如源文件所示,"C:[sample.xlsx]FinancingSchedule'!O266"但我想将其更改为 "C:newlycreated.xlsx]FinancingSchedule'!O266"

有没有办法做到这一点?提前致谢

还有前面的问题和代码:

如何将选定的工作表从excel文件复制到程序创建的新excel文件中

4

2 回答 2

2

看到这个。我已经对代码进行了注释,以便您理解它不会有任何问题。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlexcel;
            Excel.Workbook xlWorkBook;
            Excel.Workbook xlWorkBookNew;
            Excel.Sheets worksheets;

            //~~> Path to save the new file
            String NewPath = "C:\\";

            //~~> name of the newly created file
            String NewFileName = "newlycreated";

            object misValue = System.Reflection.Missing.Value;

            xlexcel = new Excel.Application();

            xlexcel.Visible = true;

            //~~> Open a File
            xlWorkBook = xlexcel.Workbooks.Open("C:\\Sample.xlsx", 0, true, 5, "", "", true,
            Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            //~~> Specify the sheets you want to copy
            String[] selectedSheets = { "a", "c", "d" };

            //~~> Set your worksheets
            worksheets = xlWorkBook.Worksheets;

            //~~>Copy it. This will create a new Excel file with selected sheets
            ((Excel.Sheets)worksheets.get_Item(selectedSheets)).Copy();

            xlWorkBookNew = xlexcel.ActiveWorkbook;

            //~~> Save the new workbook
            xlWorkBookNew.SaveAs(NewPath + NewFileName + ".xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook, 
            misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive,
            misValue, misValue, misValue, misValue, misValue);

            //~~> Loop through the sheets of the new workbook and replace "Sample.xlsx" with "newlycreated.xlsx"
            foreach (Excel.Worksheet xlsheet in xlWorkBookNew.Sheets) 
            {
                xlsheet.Cells.Replace("Sample.xlsx", NewFileName + ".xlsx", Excel.XlLookAt.xlWhole,
                Excel.XlSearchOrder.xlByRows, false, false, false);

            }

            //~~> Close and Cleanup
            xlWorkBookNew.Close(true, misValue, misValue);
            xlWorkBook.Close(false, misValue, misValue);
            xlexcel.Quit();

            releaseObject(worksheets);
            releaseObject(xlWorkBookNew);
            releaseObject(xlWorkBook);
            releaseObject(xlexcel);

        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}
于 2013-01-24T08:32:12.153 回答
0

如果我没记错的话,您已经复制了一张工作表,并且在您新创建的工作表中,您可以按原样放置所有旧工作表数据。我的意思是,如果旧工作表数据是从 cellB1 开始显示的,那么新创建的工作表也会从 cellB1 开始其数据。如果是这种情况,您必须逐个单元格地读取旧工作表数据,并且必须明确告诉新工作表哪个单元格数据(来自旧工作表)进入哪个单元格(新创建的工作表)。复制工作表非常简单且速度更快,但是如果您确实需要更改新创建的工作表数据的位置,则需要牢记性能问题,尤其是在复制大量数据的情况下。如果您真的对这种方法感兴趣,我可以为您提供代码示例。问候,

于 2013-01-23T12:18:48.060 回答