i want to read the metadata (such as author, keywords, etc.) of the Word, Excel file by asp.net (vb). How can I do that ?
is there any sample, reference site ?
i found the following code online, but it return error for the 2nd statement (" Dim excelbook As New Microsoft.Office.Interop.Excel.Workbook ")
Public Sub ReadExcel(ExcelFileName As String)
Dim Wapp As New Microsoft.Office.Interop.Excel.Application
Dim excelbook As New Microsoft.Office.Interop.Excel.Workbook
If Wapp Is Nothing Then
Wapp = New Microsoft.Office.Interop.Excel.Application
End If
If excelbook Is Nothing Then
excelbook = New Microsoft.Office.Interop.Excel.Workbook
Else
excelbook.Close()
End If
excelbook = Wapp.Workbooks.Open(ExcelFileName)
Dim _BuiltInProperties As Object = excelbook.BuiltinDocumentProperties
If Not _BuiltInProperties Is Nothing Then
excel_keyword = _BuiltInProperties("Keywords").Value
End If
If Not excelbook Is Nothing Then
excelbook.Close()
End If
If Not Wapp Is Nothing Then
Wapp.Quit()
End If
End Sub
here is the example for the same. Or you can download from https://dl.dropbox.com/u/79986486/StackOverFlow9684368.zip {Temporary}
You can use openXML with C# or VB.Net to achive this.
Hope this helps.
Quoting from Social.MSDN
You can access the values using DSOFile (works for a whole bunch of different fileTypes), or more simply, you can access these properties like this in VBA
Sub Macro1()
'
' Macro1 Macro
'
Dim mWorkbook As Workbook
Set mWorkbook = Application.Workbooks(1)
mWorkbook.BuiltinDocumentProperties("Author").Value = "the Author"
mWorkbook.BuiltinDocumentProperties("Title").Value = "the Title"
mWorkbook.BuiltinDocumentProperties("Subject").Value = "the Subject"
End Sub
Related
I'm trying to get the full path minus the filename from a path in vb.net. I'm using a textbox and a browse button to get the full path, but I want to save just the directories.
How do I get just the directories and sub-directories?
Example file name:
c:\Users\jsmith\Desktop\file.aspx
Desired result:
c:\Users\jsmith\Desktop\
Protected Sub btnBackupFolderName_Click(sender As Object, e As EventArgs) Handles btnBackupFolderName.Click
' Call ShowDialog.
Dim result As DialogResult = openFD.ShowDialog()
' Test result.
If result = Windows.Forms.DialogResult.OK Then
Dim FileNameText As String = openFD.FileName.ToString()
Dim backupFolderName = Path.GetFileName(Path.GetDirectoryName(FileNameText)) 'this just gives me 'Desktop'
txtBackupFolder.Text = di.ToString 'backupFolderName
End If
End Sub
Thank you for your help!
Get the full path of the file and then use GetDirectoryName() to retrieve the folder-path, like this:
Dim openFD As OpenFileDialog = New OpenFileDialog()
Dim result As DialogResult = openFD.ShowDialog()
If result = DialogResult.OK Then
Dim FileNameText As String = openFD.FileName.ToString()
' This gets the folder-path, sans filename.
txtBackupFolder.Text = Path.GetDirectoryName(openFD.FileName)
End If
Folks- I'm relatively new to ASP.NET, and have a question that has stumped my peers-- folks much more experienced than myself.
My company created a website that uses iTextSharp to build and stream PDFs. The functionality works perfectly on my company's development and staging/test servers. The customer's functionality isn't working well, however. The customer's server streams a file where the PDF is missing the last block of data representing the EOF section. The PDF seems to build correctly, streams correctly, but when users open the PDF, the following error displays: 'There was an error opening this document. The file is damaged and could not be repaired.'
By comparing the PDFs in a text viewer (comparing the PDFs from my server vice the customer's server), I can see that the EOF section is missing from the customer's PDF. I'll also note that no errors are thrown during PDF creation, if that's helpful. To make matters more difficult, I have no access to the customer's servers, so I won't be able to interact with the systems directly.
The asp.net version is 3.5. Both of our servers (my company and the customer) are: running IIS7.5 on Server 2008R2; using iTextSharp is 5.1.2; and are configured for FIPS compatibility.
I've read dozens and dozens of posts detailing why a PDF isn't created properly, why it may not be streaming, and all things related, but I haven't seen this particular issue before. I guess what I need to know in the short-term is: 1) what can I provide to help diagnose the issue, 2) where is a good place to start looking for areas of concern?
Also, I updated to revision 5.5.3 last night; same results-- it works fine on my servers, but produces broken PDFs on the customer's server.
Code added:
Public Function BuildReport(ByVal tblReport As DataTable, _
ByRef memStream As MemoryStream, _
ByRef strErrMsg As String) As Boolean
Dim booOK As Boolean = True
strErrMsg = String.Empty
' Create document
Try
' Create writer (listens to the document and directs PDF stream)
memStream = New MemoryStream()
Dim msWriter As PdfWriter = PdfWriter.GetInstance(_document, memStream)
msWriter.CloseStream = False
'Create header
Dim ev As New itsEvents
msWriter.PageEvent = ev
' Set document metadata
_document.AddTitle(_strMetaTitle)
_document.AddSubject(_strMetaSubject)
_document.AddCreator(_strMetaApplication)
_document.AddAuthor(_strMetaAuthor)
' Open document, add document content, close document
_document.Open()
AddReportContent(tblReport)
_document.Close()
Catch ex As Exception
booOK = False
strErrMsg = ex.Message
End Try
Return booOK
End Function
Private Sub AddReportContent(ByVal tblReport As DataTable)
' Count report columns
Dim intReportColumns As Integer = 0
For Each col As DataColumn In tblReport.Columns
If ContainedInColumnMask(col.ColumnName) Then
intReportColumns += 1
End If
Next
' Build table
Dim table As PdfPTable
Dim cell As PdfPCell
Dim phrase As Phrase
If intReportColumns >= 1 Then
' Init table
table = New PdfPTable(intReportColumns)
' Add title to table
'phrase = New Phrase(_strMetaTitle, _fontLarge)
'cell = New PdfPCell(phrase)
'cell.Colspan = intReportColumns
'cell.HorizontalAlignment = 1 ' 0=Left, 1=Centre, 2=Right
'table.AddCell(cell)
' Add column headers to table
Dim i As Integer = 0
Dim intColWidth As Integer
Dim intColWidths As Integer() = New Integer(intReportColumns - 1) {}
Dim intColWidthTotal As Integer = 0
Dim strColName As String
For Each col As DataColumn In tblReport.Columns
If ContainedInColumnMask(col.ColumnName) Then
strColName = col.ColumnName
If (col.ExtendedProperties.Item("NOTEXTEXPORT") <> True) Then
If col.ExtendedProperties.Contains("FRIENDLYNAME") Then
strColName = col.ExtendedProperties.Item("FRIENDLYNAME")
End If
End If
phrase = New Phrase(strColName, _fontMedium)
cell = New PdfPCell(phrase)
cell.BorderWidth = 1
cell.BackgroundColor = iTextSharp.text.BaseColor.LIGHT_GRAY
'cell.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY
table.AddCell(cell)
intColWidth = GetColumnWidth(col, strColName, _fontMedium.Size, _fontSmall.Size)
intColWidths(i) = intColWidth
intColWidthTotal += intColWidth
i += 1
End If
Next
table.TotalWidth = intColWidthTotal
table.SetWidths(intColWidths)
' Add rows to table
For Each row As DataRow In tblReport.Rows
For Each col As DataColumn In tblReport.Columns
If ContainedInColumnMask(col.ColumnName) Then
phrase = New Phrase(SetBlankIfNothing(row.Item(col.ColumnName).ToString()), _fontSmall)
cell = New PdfPCell(phrase)
cell.BorderWidth = 0.5
table.AddCell(cell)
End If
Next
Next
Else
' Init table
table = New PdfPTable(1)
' Nothing to add to table
table.AddCell(String.Empty)
End If
' Add table to document
_document.Add(table)
End Sub
Public Sub New(ByVal strMetaTitle As String, _
ByVal strMetaSubject As String, _
ByVal strMetaApplication As String, _
ByVal strMetaAuthor As String, _
Optional ByVal strColumnMask As String = "")
GetStaticInfo()
_strMetaTitle = strMetaTitle
_strMetaSubject = strMetaSubject
_strMetaApplication = strMetaApplication
_strMetaAuthor = strMetaAuthor
_document = New iTextSharp.text.Document(_itsPage, _itsMarginLeft, _itsMarginRight, _itsMarginTop, _itsMarginBottom)
If strColumnMask <> "" And Not strColumnMask Is Nothing Then
_strColumnMask = strColumnMask
End If
End Sub
Public Sub New(ByVal strMetaTitle As String, _
ByVal strMetaSubject As String, _
ByVal strMetaApplication As String, _
ByVal strMetaAuthor As String, _
Optional ByVal strColumnMask As String = "")
GetStaticInfo()
_strMetaTitle = strMetaTitle
_strMetaSubject = strMetaSubject
_strMetaApplication = strMetaApplication
_strMetaAuthor = strMetaAuthor
_document = New iTextSharp.text.Document(_itsPage, _itsMarginLeft, _itsMarginRight, _itsMarginTop, _itsMarginBottom)
If strColumnMask <> "" And Not strColumnMask Is Nothing Then
_strColumnMask = strColumnMask
End If
End Sub
Hello stack overflow residents! this is my first post and i'm hoping to receive some help.
I've searched but because I'm still very new, i was not able to full find/understand my answer.
I keep encountering this error:
Message: Conversion from string "" to type 'Date' is not valid. File:
~/reports/pendingshipments.aspx Function: btnExportXls_Click Stack
Trace: at
Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String
Value) at reports_default.btnExportXls_Click(Object sender, EventArgs
e) in C:\Users\jet.jones\Documents\ERIRoot\ERITitan\ERITitan.ssa\Web
Application\reports\pendingshipments.aspx.vb:line 75
Here is my code:
on App_code
**Public Function Reports_PendingShipments(ByVal intClientID As Integer, ByVal strMinDate As Date?, ByVal strMaxDate As Date?, ByVal xmlSiteID As String) As DataTable
'=================================================================================
' Author: Jet Jones
' Create date: 2013.05.28
' Description: Returns a data table with pending shipments for the sites specified
'=================================================================================
Dim objConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("Titan").ToString)
Dim cmdGet As New SqlCommand("spReports_PendingShipments", objConn)
Dim parClientID As New SqlParameter("#ClientID", SqlDbType.Int)
Dim parMinDate As New SqlParameter("#MaxDate", IIf(Not strMinDate.HasValue, DBNull.Value, strMinDate))
Dim parMaxDate As New SqlParameter("#MaxDate", IIf(Not strMaxDate.HasValue, DBNull.Value, strMaxDate))
Dim parSiteID As New SqlParameter("#Sites", SqlDbType.Xml)
Dim objAdapter As New SqlDataAdapter(cmdGet)
Dim objTable As New DataTable
parClientID.Value = intClientID
parMinDate.Value = strMinDate
parMaxDate.Value = strMaxDate
parSiteID.Value = xmlSiteID
'set up the command object
cmdGet.Connection = objConn
cmdGet.CommandType = CommandType.StoredProcedure
'add the parameters
cmdGet.Parameters.Add(parClientID)
cmdGet.Parameters.Add(parMinDate)
cmdGet.Parameters.Add(parMaxDate)
cmdGet.Parameters.Add(parSiteID)
'open the connection
objConn.Open()
'execute the query and fill the data table
objAdapter.Fill(objTable)
'return the data table
Reports_PendingShipments = objTable
'clean up
objConn.Close()
objConn = Nothing
End Function**
my aspx.vb page calls this function this way (Get the values from the query):
objTable = Reports_PendingShipments(ucClientSearch.Value,
txtMinDate.Text, txtMaxDate.Text, strSites)
I'm passing the variable strSites because the website permissions allow for users to have access to one or more site locations, and if a report is run and the user selects "All Sites" from the dropdown, I only want to send the sites they have permissions to via XML.
If I'm missing any information please let me know!
anyone's prompt response is so greatly appreciated.
The problem is that your code is expecting empty dates to be NULL, it doesn't check for empty strings. You need something like this:
if len(strMinDate)=0 then
strMinDate = "01/01/1980"
end
Not sure what you want to default the minimum date to, but you need to add code similar to the IF statement above
Be sure to add this code prior to using the variable a few lines later...
First of all, you adding MaxDate parameter twice:
Dim parMinDate As New SqlParameter("#MaxDate", IIf(Not strMinDate.HasValue, DBNull.Value, strMinDate))
Dim parMaxDate As New SqlParameter("#MaxDate", IIf(Not strMaxDate.HasValue, DBNull.Value, strMaxDate))
And moreover, then you setting parameters values wuthout check for HasValue:
parMinDate.Value = strMinDate
parMaxDate.Value = strMaxDate
Remove these lines and fix min date parameter name
Getting error “Some or all identity references could not be translated
Getting error in 8th line
Dim FolderPath As String = "D:\Account\HA\" 'Specify the folder here
Dim UserAccount As String = mailid.ToString() & "\" & pwd
Dim objDirectoryInfo As DirectoryInfo = Nothing
Dim objDirectorySecurity As DirectorySecurity = Nothing
Dim objRule As FileSystemAccessRule = Nothing
objDirectoryInfo = New DirectoryInfo(FolderPath)
objDirectorySecurity = objDirectoryInfo.GetAccessControl
objRule = New FileSystemAccessRule(UserAccount, FileSystemRights.ReadPermissions, AccessControlType.Allow)
objDirectorySecurity.AddAccessRule(objRule)
objDirectoryInfo.SetAccessControl(objDirectorySecurity)
Try these changes I have made up for you...
Public Sub AddDirectorySecurity(ByVal FolderPath As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)
' Create a new DirectoryInfoobject.
Dim objDirectoryInfo As New DirectoryInfo(FolderPath)
' Get a DirectorySecurity object that represents the current security settings.
Dim objDirectorySecurity As DirectorySecurity = objDirectoryInfo.GetAccessControl()
' Add the FileSystemAccessRule to the security settings.
objDirectorySecurity .AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))
' Set the new access settings.
objDirectoryInfo .SetAccessControl(dSecurity)
End Sub
Give this a shot and let me know how it works out for you?
Thanks!
I see the topic isn't new, but I searched many sites, found many clues, but nothing worked for me:(
I am using Crystal Reports Viewer control in my ASP.NET application. Report is quite simple, there are two parameters which I have to pass. I have two CrystalReportsSource and one CrystalReportsViewer controls on my site. When the page loads I run this snippet:
CrystalReportSource1.ReportDocument.SetParameterValue("name", Session["name"].ToString());
CrystalReportSource1.ReportDocument.SetParameterValue("code", Session["code"].ToString());
CrystalReportViewer1.ReportSource = CrystalReportSource1;
Setting source is needed, because I have two kinds of report and depending on some other session parameter I change which report should I print on screen (and which report source should I bind).
Unfortunately, this code doesn't work for me. CRViewer shows me little prompt/box saying that ~"Logging into database failed" (its only my translation, cause this is in my locale). I have no idea how to make it works. Nor my DB (Access), nor reports need credential to login (other words - I don't have to put them in any place).
Any help would be appreciated.
Since you are dynamically changing the report source, you will need to dynamically specify the logon as well.
It would be easier if your server and database are thesame. I have not tried this myself but you may give it a try.
Private Sub aMethod(ByVal name as String, ByVal sessionName as String)Handles Me.Load
Try
Dim cryRpt As New ReportDocument
Dim crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables As Tables
Dim CrTable As Table
Select Case sessionName
Case OneSessionName 'specify a session name here'
Dim rptDoc = OneSessionName
Case AnotherSessionName 'specify other session name here'
Dim rptDoc = AnotherSessionName
End Select
Dim rptDoc = CType(rptDoc, String)
cryRpt.Load(rptDoc)
Select Case sessionName
Case OneSessionName
With crConnectionInfo
.ServerName = "yourServer1Name"
.DatabaseName = "YourDB1Name"
.UserID = "yourUser1Id"
.Password = "yourPassword1"
End With
Case AnotherSessionName
With crConnectionInfo
.ServerName = "yourServer2Name"
.DatabaseName = "YourDB2Name"
.UserID = "yourUser2Id"
.Password = "yourPassword2"
End With
End Select
CrTables = cryRpt.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next
crsAllReports.PrintMode = PaperOrientation.Landscape
crsAllReports.ReportSource = cryRpt
Catch ex As Exception
lblError.Text = "No report"
lblError.Visible = True
End Try
End Sub
Note 'If your server and dbnames are thesame you may, you may not need the second switch statement. Instead just use:
With crConnectionInfo
.ServerName = "yourServerName"
.DatabaseName = "YourDBName"
.UserID = "yourUserId"
.Password = "yourPassword"
End With