Hi,
I am trying to create an excel from SharePoint list using timer job.
The file is getting downloaded with the data, but when I try to open the file, I am getting an alert as "The file format and extension of "filename.xls" don't match. the file could be corrupted or unusage. unless you trust its source. don't open it. Do you want to open it anyway?"
If I click Yes, the file opens with all data from list. But I do not want the alert message.
I have used the below code.
public override void Execute(Guid targetInstanceId)
{
string siteCollectionUrl = string.Empty;
SPWebApplication webApp = this.Parent as SPWebApplication;
siteCollectionUrl = WebConfigurationManager.OpenWebConfiguration("/", webApp.Name).AppSettings.Settings["CustomListsURL"].Value;
if (siteCollectionUrl != null && siteCollectionUrl.Trim() != string.Empty)
{
using (SPSite site = webApp.Sites[siteCollectionUrl])
{
using (SPWeb web = site.RootWeb)
{
this.DownloadLists(web);
}
}
}
}
private void DownloadLists(SPWeb web)
{
string strListName = string.Empty;
string strSiteURL = string.Empty;
SPWebApplication webApp = this.Parent as SPWebApplication;
tring strDirectory = Convert.ToString("directory path");
DirectoryInfo dir = new DirectoryInfo(@strDirectory);
dir.Create();
string strFile = strDirectory + "\\" + "filename.xls");
createExcel(strSiteURL, strListName, strFile);
}
public void createExcel(string strsiteURL, string strListName, string strFile)
{
DataTable dataTable = new DataTable();
//Adds Columns to SpreadSheet
using (SPSite site = new SPSite(strsiteURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[strListName.Trim()];
InitializeExcel(list, dataTable);
string _schemaXML = list.DefaultView.ViewFields.SchemaXml;
if (list.Items != null && list.ItemCount > 0)
{
foreach (SPListItem _item in list.Items)
{
DataRow dr = dataTable.NewRow();
foreach (DataColumn _column in dataTable.Columns)
{
if (dataTable.Columns[_column.ColumnName] != null && _item[_column.ColumnName] != null)
{
dr[_column.ColumnName] = _item[_column.ColumnName].ToString();
}
}
dataTable.Rows.Add(dr);
}
}
System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();
grid.HeaderStyle.Font.Bold = true;
grid.GridLines = GridLines.Both;
grid.DataSource = dataTable;
grid.DataBind();
using (StreamWriter streamWriter = new StreamWriter(strFile, false, Encoding.UTF8))
{
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(streamWriter))
{
grid.RenderControl(htmlTextWriter);
}
}
}
}
}
public void InitializeExcel(SPList list, DataTable _datatable)
{
if (list != null)
{
string _schemaXML = list.DefaultView.ViewFields.SchemaXml;
if (list.Items != null && list.ItemCount > 0)
{
foreach (SPListItem _item in list.Items)
{
foreach (SPField _itemField in _item.Fields)
{
if (_schemaXML.Contains(_itemField.InternalName))
{
if (_item[_itemField.InternalName] != null)
{
if (!_datatable.Columns.Contains(_itemField.InternalName))
{
_datatable.Columns.Add(new DataColumn(_itemField.StaticName, Type.GetType("System.String")));
}
}
}
}
}
}
}
}I do not want the error alert. How to fix this? Is there anything in code to be changed?
Thanks