I have been given a legacy project which uses Microsoft.Office.Interop.Excel[11.0.0.0] to import and export files. On the server MS Excel has to be running for the import/exports to work. By running I mean someone has to log on the server and open Excel via start menu (yes it's that bad!). Does anyone know why it needs to be kept open? The import/export is extremely flakey and requires a close and re-open twice a day as it keeps crashing.
Here is the code, be warned...it's horrible!
objExcelApp = New Excel.Application()
objExcelApp.DisplayAlerts = False
objExcelBook = objExcelApp.Workbooks.Open(strExcelFileName, Password:="")
If objExcelBook.Sheets.Count > 0 Then
objExcelWorkSheet = CType(objExcelBook.Sheets(1), Excel.Worksheet)
objExcelWorkSheet.Name = gstrDEFAULTSHEETNAME 'Rename the sheet
objExcelRangeHeader = CType(objExcelWorkSheet.Rows(1), Excel.Range)
objExcelWorkSheet.Cells.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone
objExcelRangeFormat = objExcelWorkSheet.UsedRange
intColumn = objExcelRangeFormat.Columns.Count
For intColumnCount = 1 To intColumn
objColumnRange = CType(objExcelRangeHeader.Columns(intColumnCount), Excel.Range)
objColumnRange.EntireColumn.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White)
objColumnRange.EntireColumn.ClearFormats()
objColumnRange.EntireRow.ClearFormats()
Next
With objExcelWorkSheet
.Name = gstrSHEET_EXCEL
.Rows.Range("1:1").Font.Bold = True
.Rows.Range("1:1").Font.Color = RGB(0, 0, 255)
.StandardWidth = 50
.Cells.WrapText = True
.EnableOutlining = True
End With
End If
objCountLink = CType(objExcelBook.LinkSources(), Object)
If Not IsNothing(objCountLink) Then
If (objCountLink.ToString.Length > 0) Then
lblErrorMessage.Visible = True
lblErrorMessage.InnerHtml = "<li> The spreadsheet you trying to import contains external links. Please ensure all links have been removed before attempting to import </li>"
objExcelBook.Close()
objExcelApp.Workbooks.Close()
objExcelApp.Quit()
objExcelApp = Nothing
Exit Sub
End If
End If
objExcelBook.Save() 'Save the excel
objExcelBook.Close()
objExcelApp.Workbooks.Close()
objExcelApp.Quit()
objExcelApp = Nothing
Consider using Open XML SDK if you deal with open XML files only. Or you may consider using any other commercial components designed for the server-side execution.
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.
You can read more about that in the Considerations for server-side Automation of Office article.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.