需要使用c#更新excelsheet的单元格 [英] Need to update cell of excelsheet using c#

查看:147
本文介绍了需要使用c#更新excelsheet的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一张excelsheet有2张即(Sheet1名称:字典,表2名称:LimitWord),现在在LimitWord我有2列(级别和限制),以及值level = 1且limit = 0。现在使用下面的代码我想更新限制文件,但它没有应用更改,也没有给出错误消息。



请帮我找到解决方案。 />
int wordlimit = 5

Hi All,
I have a excelsheet having 2 sheet namely( Sheet1 name :dictionary, sheet 2 name :LimitWord), now in LimitWord i have 2 column (level and limit), and value level=1 and limit=0. Now using below code i want to update Limit file but it is not applying the changes and not giving error message also.

Please help me to find the solution.
int wordlimit=5

String connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + FileName + " ;Persist Security Info=False;Extended Properties=Excel 8.0;";//FMT=Delimited(,)';

                string query = "Update [LimitWord$] set WordLimit=" + wordlimit + " where level= 1";

                objCon = new OleDbConnection(connString);
                cmmd = new OleDbCommand(query, objCon);
                objCon.Open();
                cmmd.CommandType = CommandType.Text;
                cmmd.ExecuteNonQuery();







任何帮助都将不胜感激。



谢谢

AP




Any help would be appreciated.

Thanks
AP

推荐答案

设置WordLimit = + wordlimit + 其中level = 1;

objCon = new OleDbConnection(connString);
cmmd = new OleDbCommand(query,objCon);
objCon。 Open();
cmmd.CommandType = CommandType.Text;
cmmd.ExecuteNonQuery();
set WordLimit=" + wordlimit + " where level= 1"; objCon = new OleDbConnection(connString); cmmd = new OleDbCommand(query, objCon); objCon.Open(); cmmd.CommandType = CommandType.Text; cmmd.ExecuteNonQuery();







任何帮助都将不胜感激。



谢谢

AP




Any help would be appreciated.

Thanks
AP


对于excel操作,我使用EPPlus.dll。它名为 Excel软件包

你可以下载该文件



使用非常简单



Im粘贴我从我的一个项目中获取的示例代码。可能对你有所帮助。它是未经编辑的代码



使用此导入声明



for excel manipulation i use EPPlus.dll. its called Excel Package
you can download that file

its is quite simple to use

Im Pasting below a sample code that i took from one of my projects. Might be of help to you. it is unedited code

use this import statement

Imports OfficeOpenXml
Sub ExcelOP(filename As String, countryCode As String, data As System.Data.DataTable)

        Using excelPackage As New ExcelPackage()

            Dim ws As ExcelWorksheet = excelPackage.Workbook.Worksheets.Add(countryCode)

            If data.Columns.Count <= 0 Then
                Label1.Text = "No Records"
            Else
                For l As Integer = 1 To data.Columns.Count - 1
                    ws.Cells(1, l).Value = HttpUtility.HtmlDecode(data.Columns(l).ToString)
                Next

                Using rng As ExcelRange = ws.Cells(1, 1, 1, data.Columns.Count - 1) 'ws.Cells("A1:K1")
                    rng.Style.Font.Bold = True
                    rng.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid
                    rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(192, 192, 192))
                End Using

                Dim totalRecords As Integer = data.Rows.Count

                For r As Integer = 0 To totalRecords - 1
                    For s As Integer = 1 To data.Columns.Count - 1
                        ws.Cells(r + 2, s).Value = HttpUtility.HtmlDecode(data.Rows(r)(s).ToString)
                    Next
                Next
                excelPackage.SaveAs(New FileInfo(filename))
                excelPackage.Dispose()
            End If
        End Using
    End Sub


这篇关于需要使用c#更新excelsheet的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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