I currently implemented a method such that it'll export to excel any RadGridView passed as a parameter. It exports completely fine, I want to enhance it by adding a Title to the first row of the excel file then append the RadGridView underneath that row. May I ask if anyone has an idea how abouts I should do that?
public static void Export(RadGridView grid)
{
const string extension = "xls";
var dialog = new SaveFileDialog
{
DefaultExt = extension,
Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
FilterIndex = 1
};
if (dialog.ShowDialog() != true)
{
return;
}
using (var stream = dialog.OpenFile())
{
var exportOptions = new GridViewExportOptions
{
Format = ExportFormat.ExcelML,
ShowColumnHeaders = true,
ShowColumnFooters = true,
ShowGroupFooters = false,
};
exportOptions.Items = (IEnumerable)grid.ItemsSource;
grid.Export(stream, exportOptions);
}
}
If it interest anyone. Here's the solution I used,
public static void ExportWithHeader(RadGridView grid, string header)
{
try
{
string extension = "xls";
SaveFileDialog dialog = new SaveFileDialog()
{
DefaultExt = extension,
Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
FilterIndex = 1,
FileName = header
};
if (dialog.ShowDialog() == true)
{
using (Stream stream = dialog.OpenFile())
{
MemoryStream ms = new MemoryStream();
grid.Export(
ms,
new GridViewExportOptions()
{
Format = ExportFormat.ExcelML,
ShowColumnHeaders = true,
ShowColumnFooters = true,
ShowGroupFooters = false,
});
ms.Seek(0, SeekOrigin.Begin);
header = String.Format(
"<Row><Cell ss:Index='1'><Data ss:Type='String'>{0}</Data></Cell></Row>", header);
StreamReader sr = new StreamReader(ms);
string msStr = sr.ReadToEnd();
msStr = msStr.Insert(msStr.IndexOf("<Row>"), header);
stream.Write(Encoding.UTF8.GetBytes(msStr), 0, msStr.Length);
}
Process.Start(dialog.FileName);
}
}
catch
{
Notification.Error("Process Busy", "Please exit excel instance.");
}
}
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.