1

所以现在我知道了如何写入 excel 文件(非常感谢你们!),我想知道是否有办法在 excel 中写入第二列。我实际上正在向这个 excel 文件发送两个不同的变量,我希望它们并排而不是彼此相邻。我没有看到任何其他关于 C 语言的问题,所以我想我会把它扔掉。如果有,请随时将问题链接给我,对于浪费空间,我深表歉意!

File * fp;
fp = fopen("C:\\Documents and Settings\\MyName\\Desktop\\Filename.csv", "w");
if(fp == NULL){
    printf("Couldn't open file\n");
    return;
}
for (j = 0; j<Variable0; j++){
fprintf(fp, "%f\n", (j+Variable1);
fprintf(fp, "%f\n", (j+Variable2);
}
4

2 回答 2

6

您不是在编写 Excel 文件,而是在编写逗号分隔值文件 (CSV)。但是,它仍然可以使用 Excel 打开。这是个很大的差异。每列用逗号分隔。每行由换行符分隔。

File * fp;
fp = fopen("C:\\Documents and Settings\\MyName\\Desktop\\Filename.csv", "w");
if(fp == NULL){
    printf("Couldn't open file\n");
    return;
}

float otherVar1 = 1.0f; // random thing you want to put in second column
float otherVar2 = 2.0f; // random thing you want to put in second column

for (j = 0; j<Variable0; j++){
    fprintf(fp, "%f,%f\n", (j+Variable1), (otherVar1));
    fprintf(fp, "%f,%f\n", (j+Variable2), (otherVar2));
}

确保你记得关闭文件!

fclose(fp);
于 2012-06-07T17:38:46.530 回答
0

对于 CSV 文件,列由逗号分隔,行由回车分隔。

例子:

Col1,Col2
Data1,Data2
于 2012-06-07T17:40:21.730 回答