/// <summary>
/// 数据导出Excel
/// </summary>
public static class AutoExcelClass
{
/// <summary>
/// 数据写入Excel
/// </summary>
public static int autoExcel()
{
//Creating new Excel.Application
Application app = new Application();
// ERROR: EXCEL couldn't be started
if (app == null)
{
return 0;
}
// Making application visible
app.Visible = true;
//Getting the workbooks collection
Workbooks workbooks = app.Workbooks;
//Adding a new workbook
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
//Getting the worksheets collection
Sheets sheets = app.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
// ERROR: worksheet == null
if (worksheet == null)
{
return 0;
}
// Setting the value for cell
// This paragraph puts the value 5 to the cell G1
Range range1 = worksheet.get_Range("G1", Missing.Value);
//ERROR: range1 == null
if (range1 == null)
{
return 0;
}
const int nCells = 5;
range1.Value2 = nCells;
//This paragraph sends single dimension array to Excel
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int[nCells];
for (int i = 0; i < array2.GetLength(0); i++)
{
array2[i] = i + 1;
}
range2.Value2 = array2;
//This paragraph sends two dimension array to Excel
Range range3 = worksheet.get_Range