VB.NET OpenXml 修改制定Worksheet的特定单元格(外加log4net应用)

VB.NET OpenXml 修改制定Worksheet的特定单元格(外加log4net应用),第1张

概述app.config <?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSection

app.config

<?xml version="1.0" enCoding="utf-8" ?><configuration>  <configSections>    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  </configSections>  <log4net>    <appender name="fileAppender" type="log4net.Appender.fileAppender">      <file value="a.log"></file>      <appendTofile value="true"></appendTofile>      <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="[%d]-%p:%c=>%m%n"></conversionPattern>      </layout>    </appender>    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">      <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="[%d]-%p:%c=>%m%n"></conversionPattern>      </layout>    </appender>    <root>      <level value="DEBUG"></level>      <appender-ref ref="fileAppender"></appender-ref>    </root>    <logger name="MyLogger">      <level value="DEBUG"></level>      <appender-ref ref="fileAppender"></appender-ref>    </logger>  </log4net></configuration>

Module1.vb
imports System.windows.Formsimports System.IOimports log4netimports documentFormat.OpenXmlimports documentFormat.OpenXml.Packagingimports documentFormat.OpenXml.Spreadsheet<Assembly: log4net.Config.XmlConfiguratorAttribute(Watch:=True)> Module Module1    Dim logger As ILog = log4net.LogManager.GetLogger(GetType(Module1))    <STAThread()>    Sub Main()        Dim OFD As New OpenfileDialog        Dim Original As String        Dim Target As String        Dim WbP As WorkbookPart        Dim Sht As Sheet        Dim WsP As WorksheetPart        Dim Shd As SheetData        Dim Row As Row        Dim Cell As Cell        OFD.Filter = "Excel document|*.xlsx"        OFD.Multiselect = False        OFD.ShowDialog()        Original = OFD.filename        logger.Info("Original file Path :" & Original)        Target = AppDomain.CurrentDomain.BaseDirectory & "Test.xlsx"        logger.Info("Target file Path :" & Target)        file.copy(Original,Target,True)        logger.Info("file copIEd")        Using SSD As Spreadsheetdocument = Spreadsheetdocument.Open(Target,True)            logger.Info("Spreadsheet document Open")            WbP = SSD.WorkbookPart            Sht = WbP.Workbook.Descendants(Of Sheet)() _                .Where(Function(S) S.name = "Sheet1").FirstOrDefault()            logger.Info("Sheet ID : " & Sht.ID.Value)            WsP = WbP.GetPartByID(Sht.ID)            If Not WsP Is nothing Then                logger.Info("Get the WorksheetPart")                Shd = WsP.Worksheet.Descendants(Of SheetData)().FirstOrDefault()                Row = Shd.Descendants(Of Row)() _                    .Where(Function(R) R.RowIndex.Value = 3).FirstOrDefault()                logger.Info("RetrIEve the Row")                If Not Row Is nothing Then                    logger.Info("Row exists")                    Cell = Row.Descendants(Of Cell)() _                        .Where(Function(C) C.CellReference = "B3") _                        .FirstOrDefault()                    logger.Info("RetrIEve the Cell")                    If Not Cell Is nothing Then                        logger.Info("Cell exists")                        Cell.CellValue.Text = "123456"                    Else                        logger.Info("Cell doesn't exist")                        Cell = New Cell                        Cell.CellReference = "B3"                        Cell.CellValue = New CellValue                        Cell.CellValue.Text = "123456"                        Row.Append(Cell)                        logger.Info("Append the cell")                    End If                    WbP.Workbook.Save()                    logger.Info("Save changes")                Else                    logger.Info("Row doesn't exist")                    Row = New Row                    Row.RowIndex = 3                    Cell = New Cell                    Cell.CellReference = "B3"                    Cell.CellValue = New CellValue                    Cell.CellValue.Text = "123456"                    Row.Append(Cell)                    logger.Info("Append the cell")                    Shd.Append(Row)                    logger.Info("Append the row")                End If            Else                logger.Error("WorksheetPart doesn't exist")            End If            Console.Writeline("Finished")            Console.ReadKey()        End Using    End SubEnd Module
总结

以上是内存溢出为你收集整理的VB.NET OpenXml 修改制定Worksheet的特定单元格(外加log4net应用)全部内容,希望文章能够帮你解决VB.NET OpenXml 修改制定Worksheet的特定单元格(外加log4net应用)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1285768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存