如何将复制的数据从网站保存到Windows窗体应用程序中的Excel工作表? [英] How to save copied data from a website to an excel sheet in a windows form application?

查看:86
本文介绍了如何将复制的数据从网站保存到Windows窗体应用程序中的Excel工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想使用C#.NET创建一个自动化软件,它分两步工作:



1.选择特定数据并从中复制网站的页面。我使用SelectAll的Web浏览器控件完成了它。数据不是HTML数据,而是数据绑定数据。

2.接下来,我想将页面的复制内容直接保存到excel表。没有要导出的数据表或网格视图,只是直接从页面复制的内容。我迫切需要这段代码的帮助。



我很感激你的直接帮助。

解决方案

< blockquote>关于复制的数据,您可以使用Clipboard类访问它... MSDN上提供了有关如何使用它的详细信息这里



要将复制的数据导出到Excel工作表,请按照下列步骤操作:



1-将复制的数据转换为二维字符串(你可以通过解析嵌套for循环中的字符串来实现这一点。)



2-创建Microsoft Excel实例并实现DisplayInExcel方法:



 使用系统; 
使用 Excel = Microsoft.Office.Interop.Excel;
使用 System.Reflection;
使用 System.Windows.Forms;
使用 Microsoft.Office.Interop.Excel;

public class ExcelDemo
{
private Excel.Application xlApp;
private Excel._Workbook xlBook;
private Excel._Worksheet xlSheet;
private object misValue = Missing.Value;

public void DisplayInExcel( string [,] Data)
{
Excel.Workbooks xlBooks;
Excel.Sheets xlSheets;
Excel.Range范围;

尝试
{
// 实例化Excel并启动新工作簿。
xlApp = new Excel.Application();
xlBooks = xlApp.Workbooks;
xlBook = xlBooks.Add(Missing.Value);
xlSheets = xlBook.Worksheets;
xlSheet =(Excel._Worksheet)xlSheets.get_Item( 1 );

// 设置保存文件的默认位置
xlApp .DefaultFilePath = @ C:\ test;

// 获取起始单元格具有地址的范围
// m_sStartingCell及其尺寸为m_iNumRows x m_iNumCols。
range = xlSheet.get_Range ( A2,misValue);
range = range.get_Resize(Data.GetLength( 0 ),Data.GetLength( 1 ));

// 将范围值设置为数组。
range.set_Value(Missing.Value,Data);

// 自动调整
xlSheet.Rows.AutoFit() ;
xlSheet.Columns.AutoFit();

// 保存工作簿
string timeStamp = DateTime.Now.ToString( ddMMyyyymmss) ;
string save = string .Concat( test1,timeStamp, 。xls );

// 另存为
xlBook.SaveAs(保存, Excel.XlFileFormat.xlWorkbookNormal,misValue,
misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,
misValue,misValue,misValue);

}
catch (例外情况)
{
// 记录异常
}
}
}


In my application, I want to create an automation software using C#.NET which works in two steps:

1. Select particular data and copy it from a page of a website. I have done it using web browser control for SelectAll. The data is not HTML data but databound data.
2. Next, I want to save the copied contents of the page to an to excel sheet directly. There is no datatable or gridview to export, just the copied content directly from a page. I urgently need help for this code section.

I'll be grateful for your immediate help.

解决方案

Concerning the copied data you can access it using the Clipboard class ... Details on how to use it are available on MSDN here

To export copied data to an excel sheet, follow these steps:

1- Convert the copied data to a 2-dimensional string (you can do this by parsing the strings in nested for loops).

2- Create an instance of Microsoft Excel and implement the DisplayInExcel method:

using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

public class ExcelDemo
{
        private Excel.Application xlApp;
        private Excel._Workbook xlBook;
        private Excel._Worksheet xlSheet;
        private object misValue = Missing.Value;

        public void DisplayInExcel(string[,] Data)
         {
            Excel.Workbooks xlBooks;
            Excel.Sheets xlSheets;
            Excel.Range range;

            try
            {
                // Instantiate Excel and start a new workbook.
                xlApp = new Excel.Application();
                xlBooks = xlApp.Workbooks;
                xlBook = xlBooks.Add(Missing.Value);
                xlSheets = xlBook.Worksheets;
                xlSheet = (Excel._Worksheet)xlSheets.get_Item(1);

                //Set the default location for saving files
                xlApp.DefaultFilePath = @"C:\test";

                //Get the range where the starting cell has the address
                //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
                range = xlSheet.get_Range("A2", misValue);
                range = range.get_Resize(Data.GetLength(0), Data.GetLength(1));

                //Set the range value to the array.
                range.set_Value(Missing.Value, Data);

                //Autofit
                xlSheet.Rows.AutoFit();
                xlSheet.Columns.AutoFit();

                //Save the workbook
                string timeStamp = DateTime.Now.ToString("ddMMyyyymmss");
                string save = string.Concat("test1", timeStamp, ".xls");

                //Save as
                xlBook.SaveAs(save, Excel.XlFileFormat.xlWorkbookNormal, misValue,
                misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue,  misValue,
                misValue, misValue, misValue);

           }
           catch(Exception ex)
             {
               //Log exception
             }
       }
}


这篇关于如何将复制的数据从网站保存到Windows窗体应用程序中的Excel工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆