• NPOI 导出Excel部分


      if (this.testDataSet1.TestTable.Rows.Count <= 0)
                {
                    MessageBox.Show("没有找到相关的数据!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DialogResult isOK = new DialogResult();
                IWorkbook workbook = null;
                ISheet sheet = null;
    
                //导出文件对话框
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Filter = "Excel 2003兼容格式|*.xls|Excel 2007文件格式|*.xlsx";    //设置文件过滤器
                saveFile.FileName = dateTimePicker2.Value.ToString("yyyy-MM-dd") + "" + dateTimePicker3.Value.ToString("yyyy-MM-dd") + "统计表";
                isOK = saveFile.ShowDialog();
                if (isOK != DialogResult.OK  || String.IsNullOrEmpty(saveFile.FileName))
                {
                    return;
                }
                else
                {
                    try
                    {
                        int rowIndex = 0;     //行号
                        workbook = CreateIWorkBook(@saveFile.FileName.ToString());     //创建Excel                     
                        FileStream fs = new FileStream(saveFile.FileName, FileMode.OpenOrCreate);        //创建文件流打开或创建
                        sheet = workbook.CreateSheet("Sheet1");     //创建sheet 
                        IRow row = null;
                        ICell cell = null;
                        //从数据集中读数据
                        int rowCount = testDataSet1.TestTable.Rows.Count;    //rowCount 行数
                        int colCount = 11;//colCount 列数
                        //创建表头
                        string[] header = { "单号", "名称", "日期", "金额", "实付金额", "销款金额", "付款方式", "银行", "单据类型", "单据号码", "备注" };     //列名
                        string[] col = { "DanHao", "MingCheng", "RiQi", "JinE", "ShiFu", "XiaoKuan", "FangShi", "YinHang", "LeiXing", "DanJuHao", "BeiZhu" };      //数据集列
                        row = sheet.CreateRow(rowIndex);
                        cell = row.CreateCell(0);
                        cell.SetCellValue("Excel文件名");
                        row = sheet.CreateRow(++rowIndex);    //rowIndex = 1;
                        //输出列名
                        for (int colIndex = 0; colIndex < colCount; ++colIndex)
                        {
                            cell = row.CreateCell(colIndex);
                            cell.SetCellValue(header[colIndex]);
                        }
                        //int rowIndex = 2;
                        foreach (DataRow dr in testDataSet1.TestTable.Rows)
                        // for (; rowIndex < rowCount+2;++rowIndex  )
                        {
                            row = sheet.CreateRow(++rowIndex);
                            for (int colIndex = 0; colIndex < 11; ++colIndex)
                            {
                                cell = row.CreateCell(colIndex);
                                //cell.SetCellValue(testDataSet1.TestTable[rowIndex-2][col[colIndex]].ToString());     
                                if (dr[col[colIndex]].GetType().Equals(typeof(System.DateTime)))     //日期格式
                                {
                                    cell.SetCellValue(Convert.ToDateTime(dr[col[colIndex]]).ToString("yyyy-MM-dd"));
                                }
                                else if (dr[col[colIndex]].GetType().Equals(typeof(System.Double)))      //Double
                                {
                                    cell.SetCellValue(Convert.ToDouble(dr[col[colIndex]]));
                                }
                                else if (dr[col[colIndex]].GetType().Equals(typeof(System.Int32)))     //Int
                                {
                                    cell.SetCellValue(Convert.ToInt32(dr[col[colIndex]]));      
                                }
                                else
                                {
                                    cell.SetCellValue(dr[col[colIndex]].ToString().Trim());
                                }
                            }
                            //++rowIndex;
                        }
                        workbook.Write(fs);     //写入文件流
                        fs.Close();       //关闭文件流
                        MessageBox.Show("文件 '" + saveFile.FileName + "' 保存成功!");
                    }   
                 catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
  • 相关阅读:
    Lock
    线程结束的正确方式
    MySQL字段自增自减的SQL语句
    PIE使用阴影后的背景透明方法
    CSS中RGBA的兼容方法以及透明度计算方法
    纯 CSS 实现三角形尖角箭头的实例
    浏览器 CSS 兼容写法的测试总结
    使用PIE对IE进行CSS3兼容介绍和经验总结
    Linux下安装vnstat流量统计
    Linux下nginx生成日志自动切割
  • 原文地址:https://www.cnblogs.com/bamboo-140/p/9690118.html
Copyright © 2020-2023  润新知