ASPose导出excel简单操作

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;
            }

  

时间: 2024-11-08 19:31:27

ASPose导出excel简单操作的相关文章

Excel导出学习之道:Java Web利用POI导出Excel简单例子

采用Spring mvc架构: Controller层代码如下 [java] view plaincopy @Controller public class StudentExportController{ @Autowired private StudentExportService studentExportService; @RequestMapping(value = "/excel/export") public void exportExcel(HttpServletReq

java导入导出excel常用操作小结及简单示例

POI中常用设置EXCEL的操作小结: 操作excel如下 HSSFWorkbook wb = new HSSFWorkbook();  //创建一个webbook,对应一个Excel文件 HSSFSheet sheet = wb.createSheet();    //添加一个sheet,对应Excel文件中的sheet 构造方法可以有参也可以无参wb.createSheet("学生表一") HSSFRow row = sheet.createRow((int) 0);  //she

C#使用Aspose.Cells导出Excel简单实现

首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出): 1 /// <summary> 2 /// 导出Excel表格 3 /// </summary> 4 /// <param name="list">数据集合</param> 5 /// <param name="heade

Aspose 导出excel小demo

//转为pdf private void CelltoPDF(string cellPath, string pdfPath) { Workbook book = new Workbook(); book.Open(cellPath); book.Save(pdfPath, FileFormatType.Pdf); } //导入 private DataTable ReadExcel(string fielPathName) { Workbook book = new Workbook(); b

java 导出excel(简单案例)

public class Student { private int id; private String name; private int age; private Date birth; public Student() { } public Student(int id, String name, int age, Date birth) { this.id = id; this.name = name; this.age = age; this.birth = birth; } ...

使用Aspose.Cell.dll导出Excel总结

这两天项目上用Aspose导出Excel来着.开始感觉挺简单的,但是实际操作起来还是挺复杂的,调试占的时间很长.主要是动态生成列.合并单元格.调样式占了很长时间,还是总结一下吧. 基础操作: //EXCEL模板路径 var filePath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.CurrentPackage.Settings["FilePath"]); //打开模板sheet

Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框

在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下.(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^) 呵呵,首先我们来导出EXCEL格式的文件吧.现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI.这里我们用Apache POI!我们先去Apach

.NET使用NPOI组件将数据导出Excel

.NPOI官方网站:http://npoi.codeplex.com/ 可以到此网站上去下载最新的NPOI组件版本 2.NPOI在线学习教程(中文版): http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html 感谢Tony Qu分享出NPOI组件的使用方法 3..NET调用NPOI组件导入导出Excel的操作类 此NPOI操作类的优点如下:   (1)支持web及winform从DataTable导出到Excel:    (2

POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】

实际开发过程中通常用到的就是从数据库导出EXCEL表格了,JXL可以这样做,其实POI也可以(关于JXL与POI的异同可访问我之前总结的文章),之前写过POI对七种文档(当然也包括EXCEL)的内容读取操作的文章,这次要写的就非常重要了,就是开发中经常会用到的POI读取数据库导出EXCEL的操作,所谓导出EXCEL也就是生成带数据内容的新的EXCEL文件 目前的POI版本是3.7 下载地址:http://poi.apache.org/download.html#POI-3.7 必须包只有一个:p