C # create an Excel file and write the content

Source: Internet
Author: User

1. added the Microsoft. Office. InterOP. Excel application.
2 reference namespace using Excel = Microsoft. Office. InterOP. Excel;
/// <Summary>
/// If the supplied Excel file does not exist then create it
/// </Summary>
/// <Param name = "FILENAME"> </param>
Private void createexcelfile (string filename)
{
// Create
Object nothing = system. reflection. Missing. value;
VaR APP = new excel. Application ();
App. Visible = false;
Excel. Workbook workbook = app. workbooks. Add (nothing );
Excel. worksheet = (Excel. worksheet) Workbook. Sheets [1];
Worksheet. Name = "work ";
// Headline
Worksheet. cells [1, 1] = "FILENAME ";
Worksheet. cells [1, 2] = "findstring ";
Worksheet. cells [1, 3] = "replacestring ";
 
Worksheet. saveas (filename, type. missing, type. missing, type. missing, type. missing, type. missing, Excel. xlsaveasaccessmode. xlnochange, type. missing, type. missing, type. missing );
Workbook. Close (false, type. Missing, type. Missing );
App. Quit ();
}
 
/// <Summary>
/// Open an Excel file, then write the content to file
/// </Summary>
/// <Param name = "FILENAME"> file name </param>
/// <Param name = "findstring"> first cloumn </param>
/// <Param name = "replacestring"> second cloumn </param>
Private void writetoexcel (string excelname, string filename, string findstring, string replacestring)
{
// Open
Object nothing = system. reflection. Missing. value;
VaR APP = new excel. Application ();
App. Visible = false;
Excel. workbook mybook = app. workbooks. open (excelname, nothing, nothing );
Excel. worksheet mysheet = (Excel. worksheet) mybook. worksheets [1];
Mysheet. Activate ();
// Get activate sheet Max row count
Int maxrow = mysheet. usedrange. Rows. Count + 1;
Mysheet. cells [maxrow, 1] = filename;
Mysheet. cells [maxrow, 2] = findstring;
Mysheet. cells [maxrow, 3] = replacestring;
Mybook. Save ();
Mybook. Close (false, type. Missing, type. Missing );
Mybook = NULL;
// Quit Excel app
App. Quit ();
}

1. added the Microsoft. Office. InterOP. Excel application.
2 reference namespace using Excel = Microsoft. Office. InterOP. Excel;
/// <Summary>
/// If the supplied Excel file does not exist then create it
/// </Summary>
/// <Param name = "FILENAME"> </param>
Private void createexcelfile (string filename)
{
// Create
Object nothing = system. reflection. Missing. value;
VaR APP = new excel. Application ();
App. Visible = false;
Excel. Workbook workbook = app. workbooks. Add (nothing );
Excel. worksheet = (Excel. worksheet) Workbook. Sheets [1];
Worksheet. Name = "work ";
// Headline
Worksheet. cells [1, 1] = "FILENAME ";
Worksheet. cells [1, 2] = "findstring ";
Worksheet. cells [1, 3] = "replacestring ";

Worksheet. saveas (filename, type. missing, type. missing, type. missing, type. missing, type. missing, Excel. xlsaveasaccessmode. xlnochange, type. missing, type. missing, type. missing );
Workbook. Close (false, type. Missing, type. Missing );
App. Quit ();
}

/// <Summary>
/// Open an Excel file, then write the content to file
/// </Summary>
/// <Param name = "FILENAME"> file name </param>
/// <Param name = "findstring"> first cloumn </param>
/// <Param name = "replacestring"> second cloumn </param>
Private void writetoexcel (string excelname, string filename, string findstring, string replacestring)
{
// Open
Object nothing = system. reflection. Missing. value;
VaR APP = new excel. Application ();
App. Visible = false;
Excel. workbook mybook = app. workbooks. open (excelname, nothing, nothing );
Excel. worksheet mysheet = (Excel. worksheet) mybook. worksheets [1];
Mysheet. Activate ();
// Get activate sheet Max row count
Int maxrow = mysheet. usedrange. Rows. Count + 1;
Mysheet. cells [maxrow, 1] = filename;
Mysheet. cells [maxrow, 2] = findstring;
Mysheet. cells [maxrow, 3] = replacestring;
Mybook. Save ();
Mybook. Close (false, type. Missing, type. Missing );
Mybook = NULL;
// Quit Excel app
App. Quit ();
}

 

C # create an Excel file and write the content

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.