Directly run the Code:
using MSExcel = Microsoft.Office.Interop.Excel;public class ExcelHelper{ /// <summary> /// Creates the excel file by column. /// </summary> /// <param name="filename">The filename.</param> /// <param name="columns">The columns.</param> public static void CreateExcelFileByColumn (string filename, IEnumerable<ColumnData> columns) { createExcelFile (filename, excelApp => { //Write data into the workbook by column. int columnIndex = 1; foreach (var column in columns) { //Write the header. excelApp.Cells[1, columnIndex].Value = column.Header; //Write the following lines in this column. int rowIndex = 2; foreach (var cell in column.Data) { excelApp.Cells[rowIndex++, columnIndex].Value = cell; } columnIndex++; } }); } /// <summary> /// Creates the excel file by row. /// </summary> /// <param name="filename">The filename.</param> /// <param name="rows">The rows.</param> public static void CreateExcelFileByRow (string filename, IEnumerable<IEnumerable> rows) { createExcelFile (filename, excelApp => { //Write data into the workbook by row. int rowIndex = 1; foreach (var row in rows) { int columnIndex = 1; foreach (var cell in row) { excelApp.Cells[rowIndex, columnIndex++].Value = cell; } rowIndex++; } }); } /// <summary> /// Creates the excel file and perform the specified action. /// </summary> /// <param name="filename">The filename.</param> /// <param name="action">The action.</param> private static void createExcelFile (string filename, Action<MSExcel.Application> action) { //Create the excel application and set it to run in background. var excelApp = new MSExcel.Application (); excelApp.Visible = false; //Add a new workbook. excelApp.Workbooks.Add (); //Perform the action. action (excelApp); //Save the workbook then close the file. excelApp.ActiveWorkbook.SaveAs (Filename: filename, FileFormat: MSExcel.XlFileFormat.xlWorkbookNormal); excelApp.ActiveWorkbook.Close (); //Exit the excel application. excelApp.Quit (); }}
This class provides two methods: Creating an excel file by column data and creating an excel file by row data.
Note that the return value of excelApp. Cells [row, column] is already the dynamic type introduced in. NET 4.0,
Instead of the original object type, you can directly call the. Value () method above, which is dynamically parsed by DLR;
In the past, we had to first convert the returned value to Range and write it:
((MSExcel.Range)excelApp.Cells[row, column]).Value2 = value;
In addition, the introduction of the named parameters and optional parameters in C #4.0 makes the method call to the COM Object equivalent.
Concise, only look at the last SaveAs method. In the past, a bunch of Type. Missing objects must be passed to indicate that this parameter is not passed.
Number or use the default value, such:
excelApp.ActiveWorkbook.SaveAs (filename, MSExcel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSExcel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
It is a headache. Now you only need to specify the parameter to be given with the name parameter. Other parameters are automatically used because they are optional.
Default Value:
excelApp.ActiveWorkbook.SaveAs (Filename: filename, FileFormat: MSExcel.XlFileFormat.xlWorkbookNormal);
The meaning is very clear.
Finally, add the definition of the simple ColumnData class:
/// <summary>/// Represents the header and data of a column./// </summary>public class ColumnData{ /// <summary> /// Gets or sets the header. /// </summary> /// <value>The header.</value> public string Header { get; set; } /// <summary> /// Gets or sets the data. /// </summary> /// <value>The data.