- PR -

VB.net から Excelファイルを出力する時エラーが発生した

1
投稿者投稿内容
lucy
会議室デビュー日: 2009/03/12
投稿数: 1
投稿日時: 2009-03-12 11:47
開発環境:XP ,Sql Sever 2005 Excel2003
サーバ環境:Windows Sever 2003,Sql Sever 2005 ,Excel2003
開発環境で問題がないです。でも、サーバ側で移行した後、いろいろな問題がありそう。
コンポーネント サービスもセットされました。
マクロを含んでいるExcelファイルをコピーして、別のファイルを作って、作成したファイルを開けて、新しいシートを追加するようにしたい場合は、以下のエラーが発生しました。
System.Runtime.InteropServices.COMException (0x80010105): サーバーによって例外が返されました。 (HRESULT からの例外: 0x80010105 (RPC_E_SERVERFAULT))
ソース
''' <summary>
''' 既存のExcelファイルに新しいシートを追加する
''' </summary>
''' <param name="dt">データ</param>
''' <param name="arrList">テーマ</param>
''' <param name="objFile">コピー元ファイルの名(パスを含む)</param>
''' <param name="oriaFile" >コピー先ファイルの名(パスを含む)</param>
''' <param name="msg" >メッセージ</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function CreateExcel(ByVal dt As Data.DataTable, ByVal arrList As ArrayList, ByVal oriaFile As String, ByVal objFile As String, ByRef msg As String) As Boolean

'Excelオブジェクトの作成
Dim oExcel As Excel.Application = Nothing
'WorkBookオブジェクト
Dim oBook As Excel.WorkbookClass = Nothing
'シートオブジェクト
Dim oSheet As Excel.Worksheet = Nothing

Dim stringLine() As String

Dim cellItem() As String
'フラグ
Dim flg As Boolean = True

Dim FilePath As String = oriaFile

Dim csvBuilder As New StringBuilder
If dt.Rows.Count > 0 Then
CreateCSVHeader(csvBuilder, arrList)
CreateCSVBody(csvBuilder, dt)
End If
Try

oExcel = New Excel.Application()
'ファイルの存在性のチェック
If System.IO.File.Exists(FilePath) Then
'ファイルをコピーする
System.IO.File.Copy(FilePath, objFile, True)
'System.IO.File.Create(objFile)
'Excelを非表示にする
oExcel.Application.Visible = false
'アラームを非表示にする
oExcel.DisplayAlerts = False

'マクロを無効にして
oExcel.EnableEvents = False

'コピー先ファイルを開く

'oBook = oExcel.Workbooks.Open(objFile, True)
oBook = oExcel.Workbooks.Open(objFile, False)
'新しいシートを追加する
oBook.Worksheets.Add(After:=oBook.Worksheets(oBook.Worksheets.Count))

oSheet = oBook.Worksheets(oBook.Worksheets.Count)
'新シートの名をつける

oSheet.Name = "未配データ"
'シートにデータをセットする
stringLine = csvBuilder.ToString.Split(vbCrLf)

If stringLine.Length <> 0 Then
For i As Integer = 0 To stringLine.Length - 1
cellItem = stringLine(i).Split(",")
If cellItem.Length <> 0 Then
For j As Integer = 0 To cellItem.Length - 1
oSheet.Cells(i + 1, j + 1) = cellItem(j)
Next
End If
Next

End If

'ファイルを保存する
oBook.SaveAs(objFile)
'oExcel.DisplayAlerts = True
msg = "ファイルが作成しました"

Else
msg = "ファイルが存在しません"
flg = False
End If
Catch ex As Exception
flg = False
msg = "ファイルが作成していません"
Throw ex
Finally
If Not oSheet Is Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
oSheet = Nothing
End If
If Not oBook Is Nothing Then
Try
oBook.Close(False)
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
oBook = Nothing
End Try
End If
If Not oExcel Is Nothing Then
Try
oExcel.Quit()
Finally
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
oExcel = Nothing
End Try

End If
End Try
Return flg
End Function
現状によって、 'アラームを非表示にする
oExcel.DisplayAlerts = False

'マクロを無効にして
oExcel.EnableEvents = False
を無効になりそうです、どうしてサーバ側で無効になりましたか?



_________________
1

スキルアップ/キャリアアップ(JOB@IT)