博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASPose导出excel简单操作
阅读量:4963 次
发布时间:2019-06-12

本文共 9844 字,大约阅读时间需要 32 分钟。

string file = FileDialogHelper.SaveExcel(string.Format("{0}.xls", excelName));            if (!string.IsNullOrEmpty(file) && dt.Rows.Count > 0)            {                try                {                    string error = "";                    DataTableToExcel(dt, file, out error);                    if (!string.IsNullOrEmpty(error))                    {                        MessageDxUtil.ShowError(string.Format("导出Excel出现错误:{0}", error));                    }                    else                    {                        if (MessageDxUtil.ShowYesNoAndTips("导出成功,是否打开文件?") == System.Windows.Forms.DialogResult.Yes)                        {                            System.Diagnostics.Process.Start(file);                        }                    }                }                catch (Exception ex)                {                    LogTextHelper.Error(ex);                    MessageDxUtil.ShowError(ex.Message);                }            }

  对ASPose的操作

error = "";            Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();            try            {                if (datatable == null)                {                    error = "DataTableToExcel:datatable 为空";                    return false;                }                //为单元格添加样式                    Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()];                //设置居中                style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;                //设置背景颜色                style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);                //style.Pattern = BackgroundType.Solid;                style.Font.IsBold = true;                //为单元格添加样式                 Aspose.Cells.Style styleHeader = wb.Styles[wb.Styles.Add()];                //设置居中                styleHeader.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;                //设置背景颜色                styleHeader.ForegroundColor = System.Drawing.Color.Gold;                styleHeader.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Medium; //应用边界线 左边界线                styleHeader.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Medium; //应用边界线 右边界线                styleHeader.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Medium; //应用边界线 上边界线                styleHeader.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Medium; //应用边界线 下边界线                styleHeader.Pattern = Aspose.Cells.BackgroundType.Solid;                styleHeader.Font.IsBold = true;                int rowIndex = 0;                //Aspose.Cells.Range range;                //Aspose.Cells.Cell cell;                //range = wb.Worksheets[0].Cells.CreateRange(0, 0, 2, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("评估项目");                range.SetStyle(styleHeader);                //range = wb.Worksheets[0].Cells.CreateRange(0, 1, 2, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("评估分值");                //range = wb.Worksheets[0].Cells.CreateRange(0, 2, 1, 3);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("评分结果(不适合项目调整前)");                //range = wb.Worksheets[0].Cells.CreateRange(1, 2, 1, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("制度健全性");                //range = wb.Worksheets[0].Cells.CreateRange(1, 3, 1, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("遵循有效性");                //range = wb.Worksheets[0].Cells.CreateRange(1, 4, 1, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("合计");                //range = wb.Worksheets[0].Cells.CreateRange(0, 5, 2, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("评分结果(不适合项目调整后)");                //range = wb.Worksheets[0].Cells.CreateRange(0, 6, 2, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("权重");                //range = wb.Worksheets[0].Cells.CreateRange(0, 7, 2, 1);                //range.Merge();                //range.SetStyle(styleHeader);                //cell = range[0, 0];                //cell.PutValue("最终结果");                Aspose.Cells.Range range1 = wb.Worksheets[0].Cells.CreateRange(0, 1, 2, 1);                range1.Merge();                Aspose.Cells.Range range2 = wb.Worksheets[0].Cells.CreateRange(0, 2, 1, 3);                range2.Merge();                Aspose.Cells.Range range3 = wb.Worksheets[0].Cells.CreateRange(0, 5, 2, 1);                range3.Merge();                Aspose.Cells.Range range4 = wb.Worksheets[0].Cells.CreateRange(0, 6, 2, 1);                range4.Merge();                Aspose.Cells.Range range5 = wb.Worksheets[0].Cells.CreateRange(0, 7, 2, 1);                range5.Merge();                wb.Worksheets[0].Cells[rowIndex, 0].PutValue("评估项目");                wb.Worksheets[0].Cells[rowIndex, 0].SetStyle(styleHeader);                wb.Worksheets[0].Cells[rowIndex, 1].PutValue("评估分值");                wb.Worksheets[0].Cells[rowIndex, 1].SetStyle(styleHeader);                wb.Worksheets[0].Cells[rowIndex, 2].PutValue("评分结果(不适合项目调整前)");                wb.Worksheets[0].Cells[rowIndex, 2].SetStyle(styleHeader);                wb.Worksheets[0].Cells[1, 2].PutValue("制度健全性");                wb.Worksheets[0].Cells[1, 2].SetStyle(styleHeader);                wb.Worksheets[0].Cells[1, 3].PutValue("遵循有效性");                wb.Worksheets[0].Cells[1, 3].SetStyle(styleHeader);                wb.Worksheets[0].Cells[1, 4].PutValue("合计");                wb.Worksheets[0].Cells[1, 4].SetStyle(styleHeader);                wb.Worksheets[0].Cells[rowIndex, 5].PutValue("评分结果(不适合项目调整后)");                wb.Worksheets[0].Cells[rowIndex, 5].SetStyle(styleHeader);                wb.Worksheets[0].Cells[rowIndex, 6].PutValue("权重");                wb.Worksheets[0].Cells[rowIndex, 6].SetStyle(styleHeader);                wb.Worksheets[0].Cells[rowIndex, 7].PutValue("最终结果");                wb.Worksheets[0].Cells[rowIndex, 7].SetStyle(styleHeader);                //styleHeader.ForegroundColor = System.Drawing.Color.LightCoral;                //wb.Worksheets[0].Cells[2, 0].PutValue("(1)");                //wb.Worksheets[0].Cells[2, 0].SetStyle(styleHeader);                //wb.Worksheets[0].Cells[2, 1].PutValue("(2)");                //wb.Worksheets[0].Cells[2, 1].SetStyle(styleHeader);                //wb.Worksheets[0].Cells[2, 2].PutValue("(3)");                //wb.Worksheets[0].Cells[2, 2].SetStyle(styleHeader);                //wb.Worksheets[0].Cells[2, 3].PutValue("(4)");                //wb.Worksheets[0].Cells[2, 3].SetStyle(styleHeader);                //wb.Worksheets[0].Cells[2, 4].PutValue("(5)=(3)+(4)");                //wb.Worksheets[0].Cells[2, 4].SetStyle(styleHeader);                //wb.Worksheets[0].Cells[2, 5].PutValue("(6)");                //wb.Worksheets[0].Cells[2, 5].SetStyle(styleHeader);                //wb.Worksheets[0].Cells[2, 6].PutValue("(7)");                //wb.Worksheets[0].Cells[2, 6].SetStyle(styleHeader);                //wb.Worksheets[0].Cells[2, 7].PutValue("(8)=(6)×(7)");                //wb.Worksheets[0].Cells[2, 7].SetStyle(styleHeader);                for (int i = 0; i < datatable.Columns.Count; i++)                {                    DataColumn col = datatable.Columns[i];                    string columnName = col.Caption ?? col.ColumnName;                    wb.Worksheets[0].Cells[rowIndex, i].PutValue(columnName);                    wb.Worksheets[0].Cells[rowIndex, i].SetStyle(style);                }                rowIndex++;                //rowIndex = 3;                foreach (DataRow row in datatable.Rows)                {                    for (int i = 0; i < datatable.Columns.Count; i++)                    {                        wb.Worksheets[0].Cells[rowIndex, i].PutValue(row[i].ToString());                        wb.Worksheets[0].Cells[rowIndex, i].SetStyle(style);                    }                    rowIndex++;                }                string ExcelHeader = "PROJECTNAME,STANDARDSCORE,SYSINTEGRITY,FOLLOWEFFECTIVENESS,TOTAL,SCORERESULT,WEIGHT,ENDRESULT";                //foreach (DataRow row in datatable.Rows)                //{                //    int i = 0;                //    foreach (string col in ExcelHeader.Split(','))                //    {                //        wb.Worksheets[0].Cells[rowIndex, i].PutValue(row[col].ToString());                //        wb.Worksheets[0].Cells[rowIndex, i].SetStyle(style);                //        i++;                //    }                //    rowIndex++;                //}                //for (int k = 0; k < datatable.Columns.Count; k++)                //{                //    wb.Worksheets[0].AutoFitColumn(k, 0, 150);                //}                wb.Worksheets[0].FreezePanes(1, 0, 1, datatable.Columns.Count);                wb.Save(filepath);                return true;            }            catch (Exception e)            {                error = error + " DataTableToExcel: " + e.Message;                return false;            }

  

转载于:https://www.cnblogs.com/shangshen/p/4775449.html

你可能感兴趣的文章
处理器管理与进程调度
查看>>
页面懒加载
查看>>
向量非零元素个数_向量范数详解+代码实现
查看>>
java zip 中文文件名乱码_java使用zip压缩中文文件名乱码的解决办法
查看>>
java if 用法详解_Java编程中的条件判断之if语句的用法详解
查看>>
kafka的java客户端_KAFKA Producer java客户端示例
查看>>
java -f_java学习笔记(一)
查看>>
java 什么题目好做_用java做这些题目
查看>>
java中的合同打印_比较方法违反了Java 7中的一般合同
查看>>
php 位运算与权限,怎么在PHP中使用位运算对网站的权限进行管理
查看>>
php include效率,php include类文件超时
查看>>
matlab sin函数 fft,matlab的fft函数的使用教程
查看>>
wcdma下行如何解扩解扰 matlab,WCDMA技术基础.ppt
查看>>
MySQL date_format() 函数
查看>>
mysql 时间处理
查看>>
mysql adddate()函数
查看>>
mysql addtime() 函数
查看>>
mysql 根据日期时间查询数据
查看>>
mysql 创建时间字段
查看>>
mysql 生成随机数rand()
查看>>