[不是问题]使用C#.net写入Excel工作表 [英] [Not a question] Writing to Excel sheet using C#.net

查看:83
本文介绍了[不是问题]使用C#.net写入Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,先生,



我有一张excel表在桌面上。现在excel表完全空白。我想用C#.net给excel表写一些值...所以请提供代码或任何相关的指导。



谢谢

Good morning Sir,

I have one excel sheet which is on desktop. Now the excel sheet is completely blank. I want to write some value into the excel sheet using C#.net... So please provide me code or Any guidance related to that.

Thank You

推荐答案

您可以使用需要Excel安装的MS Office Excel互操作程序集,或者不使用Open XML SDK。请参阅我之前的回答:如何从MS Visual Studio 2010中的添加参考添加microsoft excel 15.0对象库 [ ^ ]。



另请参阅此CodeProject文章:使用Open XML创建基本的Excel工作簿 [ ^ ]。



关于互操作程序集方法,请参阅:

< a href =http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx> http://msdn.microsoft.com/en-us/library/microsoft.office .interop.excel.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/office/ff597926.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/15s06t57.aspx [ ^ ]。



但是,请阅读微软关于使用互操作的警告:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 [ ^ ] ,

http://support.microsoft.com/kb/257757/en-us [ ^ ]。



-SA
You can use either MS Office Excel interop assembly, which requires Excel installation, or Open XML SDK, which does not. Please see my past answer: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

See also this CodeProject article: Creating basic Excel workbook with Open XML[^].

On interop assembly approach, please see:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx[^],
http://msdn.microsoft.com/en-us/library/office/ff597926.aspx[^],
http://msdn.microsoft.com/en-us/library/15s06t57.aspx[^].

However, please read some words of warning from Microsoft on the use of interop:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

—SA


这是在桌面上将数据写入excel文件的代码 -

Here is the code for writing data to excel file on desktop-
//path to file
string path = "C:\\Users\\[User_Name]\\Desktop\\[file_name].xls";
        
        #region Used Excel Objects
        MsExcel.ApplicationClass excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
        MsExcel.Workbook excelWorkbook = excelApp.Workbooks.Open(path, 0, ture, 5, "", "", false, MsExcel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); ;
        MsExcel.Sheets excelSheets = excelWorkbook.Worksheets;
        //create the object of sheet1    
        MsExcel.Worksheet xlws = (MsExcel.Worksheet)excelSheets.get_Item("sheet1");
        MsExcel.Sheets anna = excelSheets;
        #endregion

        //write data to row 1 and column 1 i.e. "A"
        xlws.Cells[1, 1] = Convert.ToString(Value);


c#在桌面上不需要excel



当你在你的系统上安装了vs并且有超过vs的参考价格


你可以创建并编写excel文件并写入代码搜索msdn example
you do not need excel on your desktop for c#

when you have installed vs on your system and have excell in refrences of vs

you can create and write excel file and write to it for code search msdn example


这篇关于[不是问题]使用C#.net写入Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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