2016-03-24 10 views
1

私はExcel用のテンプレートを持っています。私は2つの異なるセルに2つのシートを開いて書いてみたいです。私はインターネット上で検索しますが、私はコードをきれいにしようとすると、私はエラーが表示されたら、私は必要ではないコードのいくつかのものがあります。私はExcelで簡単に書いて、彼を救いたい。テンプレート.xlsを開いて2つのセルに書き込む簡単な方法は何ですか?

答えて

0

あなたはExcel相互運用のために退屈のMicrosoft Office依存を避けることができNPOIライブラリ、また、この方法を使用することができます「私は簡単な方法は、Excelで作成し、彼は、保存したいです」。

短い自家製例:

Imports NPOI 
Imports NPOI.HSSF.UserModel 
Imports NPOI.HSSF.Util 
Imports NPOI.SS.UserModel 
Imports NPOI.SS.UserModel.Charts 
Imports NPOI.SS.Util 
Imports NPOI.XSSF.UserModel 

Dim WorkBookFile As String = "C:\MyWorkBook.xlsx" 

' Create the excel workbook instance. 
Dim workbook As IWorkbook = Nothing 

' Load the workbook. 
Using file As New FileStream(WorkBookFile, FileMode.Open, FileAccess.Read) 
    workbook = New XSSFWorkbook(file) 
End Using 

' Get the first sheet. 
Dim sheet As ISheet = workbook.GetSheetAt(0) 

' Get the first row. 
Dim row As IRow = sheet.GetRow(0) 

' Create a cell. 
Dim cell As ICell = row.CreateCell(1) 

' Get the cell value. 
If String.IsNullOrEmpty(cell.StringCellValue) Then ' If value is emty then... 

Set cell value. 
    cell.SetCellValue("This is a test") 

End If 

' Save changes. 
Using sw As FileStream = File.Create(WorkBookFile) 
    workbook.Write(sw) 
End Using 

は、彼らの公式のためDocumentation始める例を参照してください。

2

POCOでハンドルが必要な場合は、私のツールNpoi.Mapperを使用できます。それはまた、NPOIに基づいています。 以下のようなC#のコードですが、簡単にVB.NETに変換することができます:

var mapper = new Mapper("Book1.xlsx"); 
mapper.Put(products, "sheet1", true/*overwrite existing rows*/); 
mapper.Put(orders, "sheet2", false/*append rows*/); 
mapper.Save("Book1.xlsx"); 
関連する問題