/// <summary>
/// AR导出execl
/// </summary>
/// <param name="_document"></param>
/// <param name="isSheet">是否多工作簿(一页一个)</param>
public void New_Export(PageDocument _document, string defaultbbmc, bool isSheet = false)
{
SaveFileDialog dialog = new SaveFileDialog();
//设置文件类型
dialog.Filter = $@"Excel 97-2003工作簿(*.xls)(含页眉页脚)|*.xls|Excel 97-2003工作簿(*.xls)|*.xls|Excel 工作簿(*.xlsx)(含页眉页脚)|*.xlsx|Excel 工作簿(*.xlsx)|*.xlsx|CSV(逗号分隔)(*.csv)|*.csv";//| Excel 启动宏的工作簿(*.xlsm)| *.xlsm
//dialog.Filter = $@"Excel 97-2003工作簿(*.xls)|*.xls|Excel 工作簿(*.xlsx)|*.xlsx";
//saveFileDialog1.FilterIndex = 1;//设置文件类型显示
dialog.FileName = defaultbbmc;//设置默认文件名
dialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
dialog.CheckPathExists = true;//检查目录
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
#region 页眉页脚
if (dialog.FilterIndex == 1 || dialog.FilterIndex == 3)//含表头
{
#region 页眉页脚
if (_document.PageReport.Report.PageFooter != null)
{
_document.PageReport.Report.PageFooter.PrintOnFirstPage = true;
_document.PageReport.Report.PageFooter.PrintOnLastPage = true;
}
if (_document.PageReport.Report.PageHeader != null)
{
_document.PageReport.Report.PageHeader.PrintOnFirstPage = true;
_document.PageReport.Report.PageHeader.PrintOnLastPage = true;
}
#endregion
}
else
{
#region 页眉页脚
if (_document.PageReport.Report.PageFooter != null)
{
_document.PageReport.Report.PageFooter.PrintOnFirstPage = false;
_document.PageReport.Report.PageFooter.PrintOnLastPage = false;
}
if (_document.PageReport.Report.PageHeader != null)
{
_document.PageReport.Report.PageHeader.PrintOnFirstPage = false;
_document.PageReport.Report.PageHeader.PrintOnLastPage = false;
}
#endregion
}
#endregion
GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
if (dialog.FilterIndex == 1 || dialog.FilterIndex == 3)//含表头
{
#region 页眉页脚
if (_document.PageReport.Report.PageFooter != null)
{
_document.PageReport.Report.PageFooter.PrintOnFirstPage = true;
_document.PageReport.Report.PageFooter.PrintOnLastPage = true;
}
if (_document.PageReport.Report.PageHeader != null)
{
_document.PageReport.Report.PageHeader.PrintOnFirstPage = true;
_document.PageReport.Report.PageHeader.PrintOnLastPage = true;
}
#endregion
}
else
{
#region 页眉页脚
if (_document.PageReport.Report.PageFooter != null)
{
_document.PageReport.Report.PageFooter.PrintOnFirstPage = false;
_document.PageReport.Report.PageFooter.PrintOnLastPage = false;
}
if (_document.PageReport.Report.PageHeader != null)
{
_document.PageReport.Report.PageHeader.PrintOnFirstPage = false;
_document.PageReport.Report.PageHeader.PrintOnLastPage = false;
}
#endregion
}
if (isSheet)//两个true就是一个工作簿一页
{
XlsExport1.MultiSheet = true;//多工作簿
XlsExport1.Pagination = true;//一个工作簿中显示多页数据(表头表位会重复)
}
else
{
XlsExport1.MultiSheet = false;//多页
XlsExport1.Pagination = false;//分页
}
XlsExport1.UseCellMerging = true;//单元格合并
XlsExport1.UseDefaultPalette = false;//使用默认调色板
XlsExport1.AutoRowHeight = true;//自动行高
XlsExport1.DisplayGridLines = true;//显示网格线
XlsExport1.RemoveVerticalSpace = true;//删除垂直空间
XlsExport1.Export(_document, dialog.FileName);
if (dialog.FilterIndex == 5)//导出csv格式
{
string _name = dialog.FileName.Replace(".csv", ".xls");
if (XLSSavesaCSV(_name, true))
XtraMessageBox.Show("导出成功!");
////载入xls文档
//Workbook workbook = new Workbook();
//workbook.LoadFromFile(_name);
////获取第一张工作表
//Worksheet sheet = workbook.Worksheets[0];
////保存为csv格式
//sheet.SaveToFile(dialog.FileName.Replace(".xls", "").Replace(".xlsx", "").Replace(".csv", "") + ".csv", ",", Encoding.Default);
//File.Delete(_name);//删除原execl文件
}
else
XtraMessageBox.Show("导出成功!");
return;
string dclj = dialog.FileName.Substring(0, dialog.FileName.LastIndexOf("\\"));
string filename = dialog.FileName.Substring(dialog.FileName.LastIndexOf("\\") + 1);
// Create an output directory.
System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(dclj);
outputDirectory.Create();
// Provide settings for your rendering output.
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
if (dialog.FilterIndex == 1 || dialog.FilterIndex == 2 || dialog.FilterIndex == 5)
excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
else
excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx;
if (isSheet)//两个true就是一个工作簿一页
{
excelSetting.MultiSheet = true;//多页
excelSetting.Pagination = true;//分页
}
else
{
excelSetting.MultiSheet = false;//多页
excelSetting.Pagination = false;//分页
}
excelSetting.UseDefaultPalette = false;//使用默认调色板
//excelSetting.UseCompression = true;//是否压缩(感觉没撒变化)
GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;
// Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
//GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(filename));
// Overwrite output file if it already exists.
outputProvider.OverwriteOutputFile = true;
_document.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());
if (dialog.FilterIndex == 5)//导出csv格式
{
string _name = dialog.FileName.Replace(".csv", ".xls");
if (XLSSavesaCSV(_name, true))
XtraMessageBox.Show("导出成功!");
////载入xls文档
//Workbook workbook = new Workbook();
//workbook.LoadFromFile(_name);
////获取第一张工作表
//Worksheet sheet = workbook.Worksheets[0];
////保存为csv格式
//sheet.SaveToFile(dialog.FileName.Replace(".xls", "").Replace(".xlsx", "").Replace(".csv", "") + ".csv", ",", Encoding.Default);
//File.Delete(_name);//删除原execl文件
}
else
XtraMessageBox.Show("导出成功!");
}
catch (Exception ee)
{
XtraMessageBox.Show("导出失败;\n" + ":文件已被打开,请换名字保存,或关闭该文件");
}
}
} |