利用excel(vba)生成一个.c文件和.h文件

Sub GenerateCFile()
    Dim FilePath As String
    Dim FileContent As String
      
    ' 设定文件路径和名称
    FilePath_C = "E:\Work\Desktop\myFile.c"
    FilePath_H = "E:\Work\Desktop\myFile.h"
    
     ' 写入文件
    Open FilePath_C For Output As #1
    Open FilePath_H For Output As #2
    
    Print #1, "//这是一个.C文件 "
    Print #2, "//这是一个.H文件 "
    
    Print #1, "#include <stdio.h>"
    Print #1, "int main()"
    Print #1, "{"
    Print #1, "  " & "//代码段"
    Print #1, "  " & "while(1);"
    Print #1, "}"
    Close #1
    
    Print #2, "//这是一个.H文件"
    Print #2, "#ifndef __MAIN_H_"
    Print #2, "#define __MAIN_H_"
    Print #2, "#endif"
    Close #2
    MsgBox "C文件已成功生成!"
End Sub

OK!这样就生成了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Excel转换为JSON格式的示例代码。注意,此代码假定Excel文件的第一行是列标题,第二行及以下是数据行。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_ROWS 1000 #define MAX_COLS 100 char buffer[1024*1024]; int main(int argc, char **argv) { if (argc != 3) { printf("Usage: excel2json inputfile outputfile\n"); return 1; } char *infile = argv[1]; char *outfile = argv[2]; FILE *fp = fopen(infile, "r"); if (!fp) { printf("Error: cannot open file %s\n", infile); return 1; } char line[1024]; int row_count = 0; int col_count = 0; char *cols[MAX_COLS]; // Read column headers if (fgets(line, sizeof(line), fp)) { char *p = strtok(line, "\t\r\n"); while (p) { cols[col_count++] = strdup(p); p = strtok(NULL, "\t\r\n"); } } // Read data rows while (fgets(line, sizeof(line), fp)) { if (row_count >= MAX_ROWS) { printf("Error: too many rows\n"); return 1; } int col_index = 0; char *p = strtok(line, "\t\r\n"); while (p && col_index < col_count) { sprintf(buffer, "\"%s\":\"%s\",", cols[col_index++], p); strcat(buffer, "\n"); fwrite(buffer, strlen(buffer), 1, stdout); p = strtok(NULL, "\t\r\n"); } row_count++; } // Write JSON output to file FILE *out = fopen(outfile, "w"); if (!out) { printf("Error: cannot create file %s\n", outfile); return 1; } fprintf(out, "{\n"); for (int i = 0; i < col_count; i++) { sprintf(buffer, "\"%s\": [", cols[i]); strcat(buffer, "\n"); fwrite(buffer, strlen(buffer), 1, out); fseek(fp, 0, SEEK_SET); // Skip header row fgets(line, sizeof(line), fp); int row_index = 0; while (fgets(line, sizeof(line), fp)) { char *p = strtok(line, "\t\r\n"); for (int j = 0; j < i; j++) { p = strtok(NULL, "\t\r\n"); } if (row_index > 0) { fwrite(",", 1, 1, out); fwrite("\n", 1, 1, out); } if (p && *p) { sprintf(buffer, "\"%s\"", p); fwrite(buffer, strlen(buffer), 1, out); } row_index++; } fwrite("\n", 1, 1, out); fwrite("],\n", 2, 2, out); } fprintf(out, "}\n"); fclose(fp); fclose(out); return 0; } ``` 这个程序将Excel文件转换为JSON格式,并将结果写入输出文件中。要使用该程序,请键入以下命令: ``` excel2json inputfile.xlsx outputfile.json ``` 其中 `inputfile.xlsx` 是要转换的Excel文件,`outputfile.json` 是输出的JSON文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值