使用C#将数据附加到现有excel文件 [英] Appending data to existing excel file using C#

查看:93
本文介绍了使用C#将数据附加到现有excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我试图将一些数据从C#中的DataGridView导出到Excel文件中。 datagridview的输入由用户填写。



目前,我的程序可以创建一个excel文件以及datagridview中给定日期的值作为其文件名。



我的问题是我似乎找不到从gridview追加数据的方法如果excel文件已经存在,它会覆盖当前的excel而不是文件。



非常感谢任何帮助/提示/建议。



谢谢:)



[已解决]



我的尝试:



解决

I'm fairly new with C# and I am trying to export some data from a DataGridView in C# into an Excel file. The inputs from the datagridview are filled in by the user.

Currently, my program can create an excel file along with the values from the datagridview with the given date as its file name.

My problem is I can't seem to find a way to append the data from the gridview IF the excel file already exists, it overwrites the current excel file instead.

Any help/tips/suggestion is highly appreciated.

Thanks :)

[solved]

What I have tried:

solved

推荐答案

检查一下,这可能会对你有所帮助,因为我在VB中做同样的事情。

如何在VB中使用EPPlus [ ^ ]



如果你想要这个要转换为C#的代码,请告诉我。
Check this out,this may help you as I am doing the same just in VB.
How to Use EPPlus in VB[^]

If you want this code to be converted to C#,let me know.


您已经在C#论坛上发布了这个使用C#将数据附加到现有的excel文件 - C#讨论板 [ ^ ]。请不要在多个论坛中发布相同的问题。
You already posted this in the C# forum at Appending data to existing excel file using C# - C# Discussion Boards[^]. Please do not post the same question in multiple forums.


int lastRow_ = 3;



我担心您在写入新数据之前忘记跳过现有数据。

你需要检测现有数据并在现有数据之后写入新数据。你可能也想不再写标题。



当你不明白你的代码在做什么或为什么它做它做的时候,答案是 debugger

使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


I fear you forgot to skip existing data before writing new data.
You need to detect existing data and write new data after existing data. you probably also want to not write headers again.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于使用C#将数据附加到现有excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆