怎么用C#实现把Excel1的数据保存到Excel2中?
我想把一张Excel总表整理到几张详细的表中,我想我的提问应该是基础,谁能教我具体怎么实现?不用数据库,而用ExcelApplication该怎么做?...
我想把一张Excel总表整理到几张详细的表中,我想我的提问应该是基础,谁能教我具体怎么实现?
不用数据库,而用ExcelApplication该怎么做? 展开
不用数据库,而用ExcelApplication该怎么做? 展开
3个回答
展开全部
打开EXCEL,然后复制,粘贴
using System;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Drawing;
public Excel.Application myExcel;
public Excel.Workbook myWorkBook;
myExcel = new Excel.Application();
myWorkBook = myExcel.Application.Workbooks.Add(true);
/// <summary>
/// 打开一个存在的Excel文件
/// </summary>
/// <param name="fileName">Excel完整路径加文件名</param>
public void Open(string fileName)
{
myExcel = new Excel.Application();
myWorkBook = myExcel.Workbooks.Add(fileName);
myFileName = fileName;
}
/// <summary>
/// 复制全部
/// </summary>
public void CopyAll()
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = worksheet.Cells;
range.Select();
range.Copy(Missing.Value);
}
/// <summary>
/// 复制一个区域
/// </summary>
/// <param name="startRow"></param>
/// <param name="startColumn"></param>
/// <param name="endRow"></param>
/// <param name="endColumn"></param>
public void Copy(int startRow, int startColumn, int endRow, int endColumn)
{
Excel.Range range = myExcel.get_Range(myExcel.Cells[startRow, startColumn], myExcel.Cells[endRow, endColumn]);
range.Select();
range.Copy(Missing.Value);
}
/// <summary>
/// 粘贴所有
/// </summary>
public void PasteAll()
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = worksheet.Cells;
worksheet.Paste(Missing.Value, Missing.Value);
//worksheet.PasteSpecial(Missing.Value, true, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
/// <summary>
/// 粘贴一个区域
/// </summary>
/// <param name="startRow"></param>
/// <param name="startColumn"></param>
/// <param name="endRow"></param>
/// <param name="endColumn"></param>
public void Paste(int startRow, int startColumn, int endRow, int endColumn)
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = myExcel.get_Range(myExcel.Cells[startRow, startColumn], myExcel.Cells[endRow, endColumn]);
worksheet.Paste(range, false);
}
using System;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Drawing;
public Excel.Application myExcel;
public Excel.Workbook myWorkBook;
myExcel = new Excel.Application();
myWorkBook = myExcel.Application.Workbooks.Add(true);
/// <summary>
/// 打开一个存在的Excel文件
/// </summary>
/// <param name="fileName">Excel完整路径加文件名</param>
public void Open(string fileName)
{
myExcel = new Excel.Application();
myWorkBook = myExcel.Workbooks.Add(fileName);
myFileName = fileName;
}
/// <summary>
/// 复制全部
/// </summary>
public void CopyAll()
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = worksheet.Cells;
range.Select();
range.Copy(Missing.Value);
}
/// <summary>
/// 复制一个区域
/// </summary>
/// <param name="startRow"></param>
/// <param name="startColumn"></param>
/// <param name="endRow"></param>
/// <param name="endColumn"></param>
public void Copy(int startRow, int startColumn, int endRow, int endColumn)
{
Excel.Range range = myExcel.get_Range(myExcel.Cells[startRow, startColumn], myExcel.Cells[endRow, endColumn]);
range.Select();
range.Copy(Missing.Value);
}
/// <summary>
/// 粘贴所有
/// </summary>
public void PasteAll()
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = worksheet.Cells;
worksheet.Paste(Missing.Value, Missing.Value);
//worksheet.PasteSpecial(Missing.Value, true, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
/// <summary>
/// 粘贴一个区域
/// </summary>
/// <param name="startRow"></param>
/// <param name="startColumn"></param>
/// <param name="endRow"></param>
/// <param name="endColumn"></param>
public void Paste(int startRow, int startColumn, int endRow, int endColumn)
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = myExcel.get_Range(myExcel.Cells[startRow, startColumn], myExcel.Cells[endRow, endColumn]);
worksheet.Paste(range, false);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询