
#新建 Excel 工作表
#使用封装的 Excel 函数来实现

#预处理函数: 设置脚本加载后自动启动
#mkscript_config_auto_start()   

#预处理函数: 设置MKScript的窗口大小
mkscript_window_initial_size(600, 400) 
 
#初始化COM库
com_initialize()
 
#创建 COM 组件对象 Excel.Application
int excelApp = com_object_new("Excel.Application")
#println("excelApp:" + excelApp)
 
if excelApp > 0
 
    #COM 组件对象 Excel.Application 成功创建    
 
    #输出当前 Excel 版本信息    
    println("excel version:" + excel_get_version(excelApp))
    
    #关闭Excel相关的提示和警告消息    
    excel_display_alert(excelApp, 0)
    
    #显示Excel软件窗口界面    
    #excel_show_window(excelApp)
    
    #隐藏Excel软件窗口界面    
    excel_hide_window(excelApp)  
    
    #获取 Workbooks 对象句柄    
    int workbooks = excel_get_workbooks(excelApp)
    #println("workbooks:" + workbooks)    
    
    #创建一个新的工作簿。新工作簿将成为活动工作簿。
    int workbook = workbooks_add(workbooks)    
    #println("workbook:" + workbook)
    
    int worksheets = workbook_get_worksheets(workbook)  
    println("工作表数量:" + worksheets_get_count(worksheets))    
    
    #获取工作表集合(worksheets)中第一个工作表(worksheet)
    int worksheet = worksheets_get_index(worksheets, 1)
    
    #选中当前工作表
    worksheet_select(worksheet)
    
    #设置工作表的名称
    worksheet_set_name(worksheet, "学生")
    
    #使用 worksheet_set_range_value_xxx() 来设置单元格的内容
    worksheet_set_range_value_string(worksheet, "A1", "姓名")
    worksheet_set_range_value_string(worksheet, "B1", "身高(m)")
    worksheet_set_range_value_string(worksheet, "C1", "体重(kg)")
    worksheet_set_range_value_string(worksheet, "D1", "出生日期")
    
    #设置单元格字体样式: 粗体
    # worksheet_set_range_font_style(worksheet, "A1", "bold")
    # worksheet_set_range_font_style(worksheet, "B1", "bold")
    # worksheet_set_range_font_style(worksheet, "C1", "bold")
    # worksheet_set_range_font_style(worksheet, "D1", "bold")    
    worksheet_set_range_font_style(worksheet, "A1:D1", "bold")
    
    #使用 worksheet_set_cell_value_xxx() 来设置单元格的内容
    worksheet_set_cell_value_string(worksheet, 2, 1, "张三")
    worksheet_set_cell_value_double(worksheet, 2, 2, 1.56)
    worksheet_set_cell_value_int(worksheet,    2, 3, 38)
    worksheet_set_cell_value_date(worksheet,   2, 4, date(2010, 3, 6))
    
    #换行输出
    println("")
    
    #读取单元格的数据类型
    println("Cell(2, 1) - type:" + worksheet_get_cell_value_type(worksheet, 2, 1) + "(" + worksheet_get_cell_value_type_name(worksheet, 2, 1) + ")")
    println("Cell(2, 2) - type:" + worksheet_get_cell_value_type(worksheet, 2, 2) + "(" + worksheet_get_cell_value_type_name(worksheet, 2, 2) + ")")
    println("Cell(2, 3) - type:" + worksheet_get_cell_value_type(worksheet, 2, 3) + "(" + worksheet_get_cell_value_type_name(worksheet, 2, 3) + ")")
    println("Cell(2, 4) - type:" + worksheet_get_cell_value_type(worksheet, 2, 4) + "(" + worksheet_get_cell_value_type_name(worksheet, 2, 4) + ")")
    println("Cell(2, 5) - type:" + worksheet_get_cell_value_type(worksheet, 2, 5) + "(" + worksheet_get_cell_value_type_name(worksheet, 2, 5) + ")")    
    
    
    #设置一个单元格的文字颜色
    worksheet_set_cell_color(worksheet, 2, 3, rgb(255, 0, 0))
    
    #使用 worksheet_set_range_value_xxx() 来设置单元格的内容
    worksheet_set_range_value_string(worksheet, "A3", "李四")
    worksheet_set_range_value_double(worksheet, "B3", 1.51)
    worksheet_set_range_value_int(worksheet, "C3", 36)
    worksheet_set_range_value_date(worksheet, "D3", date(2010, 6, 2))
    
    #换行输出
    println("")
    
    #读取单元格的数据类型
    println("A3 - type:" + worksheet_get_range_value_type(worksheet, "A3") + "(" + worksheet_get_range_value_type_name(worksheet, "A3") + ")")
    println("B3 - type:" + worksheet_get_range_value_type(worksheet, "B3") + "(" + worksheet_get_range_value_type_name(worksheet, "B3") + ")")
    println("C3 - type:" + worksheet_get_range_value_type(worksheet, "C3") + "(" + worksheet_get_range_value_type_name(worksheet, "C3") + ")")
    println("D3 - type:" + worksheet_get_range_value_type(worksheet, "D3") + "(" + worksheet_get_range_value_type_name(worksheet, "D3") + ")")
    println("E3 - type:" + worksheet_get_range_value_type(worksheet, "E3") + "(" + worksheet_get_range_value_type_name(worksheet, "E3") + ")")
    
    
    #设置多个单元格的文字颜色
    worksheet_set_range_color(worksheet, "A3:C3", rgb(0,0,255))
    
    #设置多个单元格的文字颜色索引
    #worksheet_set_range_color_index(worksheet, "A3:D3", 5)
    
    println("\r\n新建的学生记录:")
    
    #遍历单元格中的学生记录, 从第2行开始读取
    int row
    for row = 2 to 3
    
        #读取单元格的内容
        string name = worksheet_get_cell_value_string(worksheet, row, 1)
        double height = worksheet_get_cell_value_double(worksheet, row, 2)
        int weight = worksheet_get_cell_value_int(worksheet, row, 3)
        double birthday = worksheet_get_cell_value_date(worksheet, row, 4)
        
        println("姓名:" + name + ", 身高:" + dtos(height, 2) + "m, 体重:" + weight + "kg, 出生日期:" + get_date(birthday))
    
    next
    
    
    #保存工作表    
    string excelSaveTo = get_script_folder_path()  + "\\new_1.xls"
    workbook_save_as(workbook, excelSaveTo)
    
    println("")
    println("已保存到 " + excelSaveTo)
    
    #退出 excel
    excel_quit(excelApp)
    
    #释放 Excel.Application 对象
    com_object_free(excelApp)    
    
    println("")
    println("完成")

else

    println("创建 COM 对象 Excel.Application 失败，返回的错误信息：" + get_last_error())

endif

