Exemple #1
0
        private bool export2Template(string filePath, List <object> rowTags, DetailExportTemplateAction action, int startIndex)
        {
            CSharpExcel excel = new CSharpExcel();

            try
            {
                excel.Open(filePath);

                if (!excel.ActivateSheet("Template"))
                {
                    return(false);
                }

                //设置单元格格式,防止科学计数
                excel.SetWriteDataFormat(3, 1, rowTags.Count + 3, 46);

                if (rowTags.Count > 0)
                {
                    for (int i = 0; i < rowTags.Count; i++)
                    {
                        action(rowTags[i], excel, i + startIndex);
                        //excel.WriteData(devInfo.strAssetCode, i + start, 0);
                    }
                }

                if (false == excel.SaveAs(filePath, true))
                {
                    if (ExportErrorInfoEvent != null)
                    {
                        ExportErrorInfoEvent("文件保存失败, 请确认文件未被占用 !");
                    }
                    return(false);
                }

                excel.Close();
                return(true);
            }
            catch (Exception ex)
            {
                Log.Write.Error(ex.Message);
            }
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// 导出数据
        /// </summary>
        public static bool ExportInfoData(string strFileName, DataGridView uvDataGridView1, string title = "标题")
        {
            CSharpExcel xlsApp = new CSharpExcel();

            if (strFileName.ToLower().EndsWith(".xls"))
            {
                xlsApp.CreateExcel(ExcelFileType.XLS);
            }
            else
            {
                xlsApp.CreateExcel(ExcelFileType.XLSX);
            }

            /* 统计图表sheet */
            xlsApp.ReNameSheet(0, title);

            int c = 0;

            /* 写入列名 */
            for (int i = 0; i < uvDataGridView1.Columns.Count; i++)
            {
                if (uvDataGridView1.Columns[i].Visible)
                {
                    xlsApp.WriteData(uvDataGridView1.Columns[i].HeaderText, 0, c++);
                }
            }

            try
            {
                /* 按行写入所有数据 */
                for (int r = 0; r < uvDataGridView1.Rows.Count; r++)
                {
                    c = 0;
                    for (int i = 0; i < uvDataGridView1.Columns.Count; i++)
                    {
                        if (uvDataGridView1.Columns[i].Visible)
                        {
                            if (null != uvDataGridView1.Rows[r].Cells[uvDataGridView1.Columns[i].Name].Value)
                            {
                                xlsApp.WriteData(uvDataGridView1.Rows[r].Cells[uvDataGridView1.Columns[i].Name].Value.ToString(), r + 1, c++);
                            }
                            else
                            {
                                xlsApp.WriteData("-", r + 1, c++);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write.Error(ex.Message);
            }


            //    //加单引号限制显示为文本模式
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["policeName"].Value.ToString(), r + 1, 0);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["eyeCameraTotal1"].Value.ToString(), r + 1, 1);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["cameraTrb1"].Value.ToString(), r + 1, 2);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["eCameraTotal1"].Value.ToString(), r + 1, 3);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["eTrouble"].Value.ToString(), r + 1, 4); ;
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["cameraTotal2"].Value.ToString(), r + 1, 5);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["cameraTrb2"].Value.ToString(), r + 1, 6);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["yidongCameraTotal"].Value.ToString(), r + 1, 7);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["yingdongTrb"].Value.ToString(), r + 1, 8);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["liantongCameara"].Value.ToString(), r + 1, 9); ;
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["liantongTrb"].Value.ToString(), r + 1, 10);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["cameraTotalNum"].Value.ToString(), r + 1, 11);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["trbTotalNum"].Value.ToString(), r + 1, 12);
            //    xlsApp.WriteData(uvDataGridView1.Rows[r].Cells["trbRate"].Value.ToString(), r + 1, 13);
            //}

            //行标题居中加粗
            xlsApp.SetCellStyle(0, 0, 0, (c - 1), 1, true, true, false, 12);

            if (false == xlsApp.SaveAs(strFileName, true))
            {
                xlsApp.Close();
                return(false);
            }
            else
            {
                return(true);
            }
        }