/// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool XLSConvertToPDF(string sourcePath, string targetPath)
{
Logger.Info("开始转pdf");
bool result = false;
XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application application = null;
Microsoft.Office.Interop.Excel.Workbook workBook = null;
try
{
application = new Application();
application.Interactive = false;
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
application.Interactive = true;
workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch(Exception ex)
{
Logger.Error("excel转pdf异常,异常信息:" + ex.Message + "。堆栈信息:" + ex.StackTrace);
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
[HttpPost]
public JsonResult UploadFile()
{
var strRes = string.Empty;
var oFile = Request.Files["txt_file"];
Workbook book = new Workbook();
book.LoadFromStream(oFile.InputStream);
var strFullName = @"D:\Data\Upload\" + "First" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
book.SaveToPdf(strFullName);
return Json(new object { }, JsonRequestBehavior.AllowGet);
}
// 根据文件路径生成workbook. public void LoadFromFile(string fileName); // 根据文件流生成workbook. public void LoadFromStream(Stream stream);
[HttpPost]
public JsonResult UploadFile()
{
var strRes = string.Empty;
var oFile = Request.Files["txt_file"];
Workbook book = new Workbook();
book.LoadFromStream(oFile.InputStream);
var strFullName = @"D:\Data\Upload\" + "First" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
pdfDocument.PageSettings.Width = 1800;//指定PDF的宽度
pdfDocument.PageSettings.Height = 1000;//指定PDF的高度
PdfConverterSettings settings = new PdfConverterSettings();
settings.TemplateDocument = pdfDocument;
PdfConverter pdfConverter = new PdfConverter(book);
pdfDocument = pdfConverter.Convert(settings);
pdfDocument.SaveToFile(strFullName);
return Json(new object { }, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult ExportData()
{
try
{
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
var random = new Random();
var iCellcount = 1;
//1.设置表头
sheet.Range[1, iCellcount++].Text = "部门名称";
sheet.Range[1, iCellcount++].Text = "部门人数";
var lstDeptName = new List<string>() { "市场部", "策划部", "公关部", "行政部", "开发部" };
var a = 0;
//2.构造表数据
for (var i = 2; i < 7; i++)
{
iCellcount = 1;
sheet.Range[i, iCellcount++].Text = lstDeptName[a++];
sheet.Range[i, iCellcount++].NumberValue = random.Next(1, 100); ;
}
//3.生成图表
SetChart(sheet, ExcelChartType.BarClustered);var strFullName = @"D:\Data\Upload\" + "Export" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
book.SaveToFile(strFullName, ExcelVersion.Version2010);
}
catch (Exception ex)
{ }
return Json(true, JsonRequestBehavior.AllowGet);
}
private void SetChart(Worksheet sheet, ExcelChartType chartFormat)
{
//1.设置sheet页的名称
sheet.Name = "Chart data";
sheet.GridLinesVisible = false;
Chart chart = sheet.Charts.Add();
//2.指定生成图表的区域
chart.DataRange = sheet.Range["A1:B6"];
chart.SeriesDataFromRange = false;
//3.指定图表的所在位置
chart.LeftColumn = 5;
chart.TopRow = 2;
chart.RightColumn = 11;
chart.BottomRow = 29;
chart.ChartType = chartFormat;
//4.设置图表的名称以及x、y轴的名称
chart.ChartTitle = "部门信息";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
chart.PrimaryCategoryAxis.Title = "部门";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
chart.PrimaryValueAxis.Title = "人数";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
//5.设置图表的值
Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
cs.CategoryLabels = sheet.Range["A2:A6"];
cs.Values = sheet.Range["B2:B6"];
cs.DataFormat.ShowActiveValue = true;
chart.Legend.Position = LegendPositionType.Top;
}
[HttpPost]
public JsonResult ExportData()
{
try
{
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
var random = new Random();
var iCellcount = 1;
//1.设置表头
sheet.Range[1, iCellcount++].Text = "部门名称";
sheet.Range[1, iCellcount++].Text = "在职人数";
sheet.Range[1, iCellcount++].Text = "离职人数";
var lstDeptName = new List<string>() { "市场部", "策划部", "公关部", "行政部", "开发部" };
var a = 0;
//2.构造表数据
for (var i = 2; i < 7; i++)
{
iCellcount = 1;
sheet.Range[i, iCellcount++].Text = lstDeptName[a++];
sheet.Range[i, iCellcount++].NumberValue = random.Next(1, 100);
sheet.Range[i, iCellcount++].NumberValue = random.Next(1, 100); ;
}
//3.生成图表
SetChart(sheet, ExcelChartType.BarClustered);
var strFullName = @"D:\Data\Upload\" + "Export" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
book.SaveToFile(strFullName, ExcelVersion.Version2010);
}
catch (Exception ex){}
return Json(true, JsonRequestBehavior.AllowGet);
}
private void SetChart(Worksheet sheet, ExcelChartType chartFormat)
{
//1.设置sheet页的名称
sheet.Name = "Chart data";
sheet.GridLinesVisible = false;
Chart chart = sheet.Charts.Add();
//2.指定生成图表的区域
chart.DataRange = sheet.Range["A1:C6"];
chart.SeriesDataFromRange = false;
//3.指定图表的所在位置
chart.LeftColumn = 5;
chart.TopRow = 2;
chart.RightColumn = 11;
chart.BottomRow = 29;
chart.ChartType = chartFormat;
//4.设置图表的名称以及x、y轴的名称
chart.ChartTitle = "部门信息";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
chart.PrimaryCategoryAxis.Title = "部门";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
chart.PrimaryValueAxis.Title = "人数";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
//5.设置图表的值
Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
cs.DataFormat.ShowActiveValue = true;
cs.DataFormat.ShowBubble = true;
chart.Legend.Position = LegendPositionType.Top;
}
{
// 摘要:
// Chart types.
public enum ExcelChartType
{
// 摘要:
// Represents the column clustered chart type.
ColumnClustered = 0,
//
// 摘要:
// Represents the stacked column chart type.
ColumnStacked = 1,
//
// 摘要:
// Represents the 100% stacked column chart type.
Column100PercentStacked = 2,
//
// 摘要:
// Represents the 3D clustered column chart type.
Column3DClustered = 3,
//
// 摘要:
// Represents the 3D stacked column chart type.
Column3DStacked = 4,
//
// 摘要:
// Represents the 3D 100% stacked column chart type.
Column3D100PercentStacked = 5,
//
// 摘要:
// Represents the 3D column chart type.
Column3D = 6,
//
// 摘要:
// Represents the clustered bar chart type.
BarClustered = 7,
//
// 摘要:
// Represents the stacked bar chart type.
BarStacked = 8,
//
// 摘要:
// Represents the 100% stacked bar chart type.
Bar100PercentStacked = 9,
//
// 摘要:
// Represents the 3D clustered bar chart type.
Bar3DClustered = 10,
//
// 摘要:
// Represents the 3D stacked bar chart type.
Bar3DStacked = 11,
//
// 摘要:
// Represents the 100% 3D stacked bar chart type.
Bar3D100PercentStacked = 12,
//
// 摘要:
// Represents the Line chart type.
Line = 13,
//
// 摘要:
// Represents the stacked line chart type.
LineStacked = 14,
//
// 摘要:
// Represents the 100% stacked line chart type.
Line100PercentStacked = 15,
//
// 摘要:
// Represents the markers line chart type.
LineMarkers = 16,
//
// 摘要:
// Represents the stacked markers line chart type.
LineMarkersStacked = 17,
//
// 摘要:
// Represents the 100% stacked markers line chart type.
LineMarkers100PercentStacked = 18,
//
// 摘要:
// Represents the 3D line chart type.
Line3D = 19,
//
// 摘要:
// Represents the pie chart type.
Pie = 20,
//
// 摘要:
// Represents the 3D pie chart type.
Pie3D = 21,
//
// 摘要:
// Represents the pie of pie chart type.
PieOfPie = 22,
//
// 摘要:
// Represents the exploded pie chart type.
PieExploded = 23,
//
// 摘要:
// Represents the 3D exploded pie chart type.
Pie3DExploded = 24,
//
// 摘要:
// Represents the bar pie chart type.
PieBar = 25,
//
// 摘要:
// Represents the markers scatter chart type.
ScatterMarkers = 26,
//
// 摘要:
// Represents the ScatterSmoothedLineMarkers chart type.
ScatterSmoothedLineMarkers = 27,
//
// 摘要:
// Represents the ScatterSmoothedLine chart type.
ScatterSmoothedLine = 28,
//
// 摘要:
// Represents the ScatterLineMarkers chart type.
ScatterLineMarkers = 29,
//
// 摘要:
// Represents the ScatterLine chart type.
ScatterLine = 30,
//
// 摘要:
// Represents the Area chart type.
Area = 31,
//
// 摘要:
// Represents the AreaStacked chart type.
AreaStacked = 32,
//
// 摘要:
// Represents the Area100PercentStacked chart type.
Area100PercentStacked = 33,
//
// 摘要:
// Represents the Area3D chart type.
Area3D = 34,
//
// 摘要:
// Represents the Area3DStacked chart type.
Area3DStacked = 35,
//
// 摘要:
// Represents the Area3D100PercentStacked chart type.
Area3D100PercentStacked = 36,
//
// 摘要:
// Represents the Doughnut chart type.
Doughnut = 37,
//
// 摘要:
// Represents the DoughnutExploded chart type.
DoughnutExploded = 38,
//
// 摘要:
// Represents the Radar chart type.
Radar = 39,
//
// 摘要:
// Represents the RadarMarkers chart type.
RadarMarkers = 40,
//
// 摘要:
// Represents the RadarFilled chart type.
RadarFilled = 41,
//
// 摘要:
// Represents the Surface3D chart type.
Surface3D = 42,
//
// 摘要:
// Represents the Surface3DNoColor chart type.
Surface3DNoColor = 43,
//
// 摘要:
// Represents the SurfaceContour chart type.
SurfaceContour = 44,
//
// 摘要:
// Represents the SurfaceContourNoColor chart type.
SurfaceContourNoColor = 45,
//
// 摘要:
// Represents the Bubble chart type.
Bubble = 46,
//
// 摘要:
// Represents the Bubble3D chart type.
Bubble3D = 47,
//
// 摘要:
// Represents the StockHighLowClose chart type.
StockHighLowClose = 48,
//
// 摘要:
// Represents the StockOpenHighLowClose chart type.
StockOpenHighLowClose = 49,
//
// 摘要:
// Represents the StockVolumeHighLowClose chart type.
StockVolumeHighLowClose = 50,
//
// 摘要:
// Represents the StockVolumeOpenHighLowClose chart type.
StockVolumeOpenHighLowClose = 51,
//
// 摘要:
// Represents the CylinderClustered chart type.
CylinderClustered = 52,
//
// 摘要:
// Represents the CylinderStacked chart type.
CylinderStacked = 53,
//
// 摘要:
// Represents the Cylinder100PercentStacked chart type.
Cylinder100PercentStacked = 54,
//
// 摘要:
// Represents the CylinderBarClustered chart type.
CylinderBarClustered = 55,
//
// 摘要:
// Represents the CylinderBarStacked chart type.
CylinderBarStacked = 56,
//
// 摘要:
// Represents the CylinderBar100PercentStacked chart type.
CylinderBar100PercentStacked = 57,
//
// 摘要:
// Represents the Cylinder3DClustered chart type.
Cylinder3DClustered = 58,
//
// 摘要:
// Represents the ConeClustered chart type.
ConeClustered = 59,
//
// 摘要:
// Represents the ConeStacked chart type.
ConeStacked = 60,
//
// 摘要:
// Represents the Cone100PercentStacked chart type.
Cone100PercentStacked = 61,
//
// 摘要:
// Represents the ConeBarClustered chart type.
ConeBarClustered = 62,
//
// 摘要:
// Represents the ConeBarStacked chart type.
ConeBarStacked = 63,
//
// 摘要:
// Represents the ConeBar100PercentStacked chart type.
ConeBar100PercentStacked = 64,
//
// 摘要:
// Represents the Cone3DClustered chart type.
Cone3DClustered = 65,
//
// 摘要:
// Represents the PyramidClustered chart type.
PyramidClustered = 66,
//
// 摘要:
// Represents the PyramidStacked chart type.
PyramidStacked = 67,
//
// 摘要:
// Represents the Pyramid100PercentStacked chart type.
Pyramid100PercentStacked = 68,
//
// 摘要:
// Represents the PyramidBarClustered chart type.
PyramidBarClustered = 69,
//
// 摘要:
// Represents the PyramidBarStacked chart type.
PyramidBarStacked = 70,
//
// 摘要:
// Represents the PyramidBar100PercentStacked chart type.
PyramidBar100PercentStacked = 71,
//
// 摘要:
// Represents the Pyramid3DClustered chart type.
Pyramid3DClustered = 72,
//
// 摘要:
// Represents the CombinationChart chart types.
CombinationChart = 73,
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有