Beispiel #1
0
        //private void AddCurrentRowStationInfo(GridViewRowInfo rowInfo, List<MesService.TestLogResultHistory> historyList,string processName,string pid,string sid)
        //{
        //    var stationInDate_burn = rowInfo.Cells[5].Value.ToString();
        //    var stationInDate_sen = rowInfo.Cells[15].Value.ToString();
        //    var stationInDate_shell = rowInfo.Cells[27].Value.ToString();
        //    var stationInDate_air = rowInfo.Cells[41].Value.ToString();
        //    var stationInDate_stent = rowInfo.Cells[46].Value.ToString();
        //    var stationInDate_product = rowInfo.Cells[55].Value.ToString();

        //    if (!string.IsNullOrEmpty(stationInDate_burn))
        //    {
        //        MesService.TestLogResultHistory history = new MesService.TestLogResultHistory();
        //        history.ProcessName = processName;
        //        history.PcbaSN = pid;
        //        history.ProductSN = sid;
        //        history.StationName = "烧录工站";
        //        history.StationInDate = stationInDate_burn;
        //        historyList.Add(history);
        //    }
        //    if (!string.IsNullOrEmpty(stationInDate_sen))
        //    {
        //        MesService.TestLogResultHistory history = new MesService.TestLogResultHistory();
        //        history.ProcessName = processName;
        //        history.PcbaSN = pid;
        //        history.ProductSN = sid;
        //        history.StationName = "灵敏度测试工站";
        //        history.StationInDate = stationInDate_sen;
        //        historyList.Add(history);
        //    }
        //    if (!string.IsNullOrEmpty(stationInDate_shell))
        //    {
        //        MesService.TestLogResultHistory history = new MesService.TestLogResultHistory();
        //        history.ProcessName = processName;
        //        history.PcbaSN = pid;
        //        history.ProductSN = sid;
        //        history.StationName = "外壳装配工站";
        //        history.StationInDate = stationInDate_shell;
        //        historyList.Add(history);
        //    }
        //    if (!string.IsNullOrEmpty(stationInDate_air))
        //    {
        //        MesService.TestLogResultHistory history = new MesService.TestLogResultHistory();
        //        history.ProcessName = processName;
        //        history.PcbaSN = pid;
        //        history.ProductSN = sid;
        //        history.StationName = "气密测试工站";
        //        history.StationInDate = stationInDate_air;
        //        historyList.Add(history);
        //    }
        //    if (!string.IsNullOrEmpty(stationInDate_stent))
        //    {
        //        MesService.TestLogResultHistory history = new MesService.TestLogResultHistory();
        //        history.ProcessName = processName;
        //        history.PcbaSN = pid;
        //        history.ProductSN = sid;
        //        history.StationName = "支架装配工站";
        //        history.StationInDate = stationInDate_stent;
        //        historyList.Add(history);
        //    }
        //    if (!string.IsNullOrEmpty(stationInDate_product))
        //    {
        //        MesService.TestLogResultHistory history = new MesService.TestLogResultHistory();
        //        history.ProcessName = processName;
        //        history.PcbaSN = pid;
        //        history.ProductSN = sid;
        //        history.StationName = "成品测试工站";
        //        history.StationInDate = stationInDate_product;
        //        historyList.Add(history);
        //    }
        //}

        #endregion

        private string ExportDesFilePath(GridViewExport.ExportFormat exportFormat)
        {
            var filter = "Excel (*.xls)|*.xls";
            var path   = "";

            if (exportFormat == GridViewExport.ExportFormat.EXCEL)
            {
                filter = "Excel (*.xls)|*.xls";
                path   = FileSelect.SaveAs(filter, "C:\\");
            }
            else if (exportFormat == GridViewExport.ExportFormat.HTML)
            {
                filter = "Html File (*.htm)|*.htm";
                path   = FileSelect.SaveAs(filter, "C:\\");
            }
            else if (exportFormat == GridViewExport.ExportFormat.PDF)
            {
                filter = "PDF file (*.pdf)|*.pdf";
                path   = FileSelect.SaveAs(filter, "C:\\");
            }
            else if (exportFormat == GridViewExport.ExportFormat.CSV)
            {
                filter = "CSV file (*.csv)|*.csv";
                path   = FileSelect.SaveAs(filter, "C:\\");
            }
            return(path);
        }
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="selectIndex"></param>
        /// <param name="radGridView"></param>
        public static void ExportGridViewData(ExportFormat exportFormat, RadGridView radGridView)
        {
            if (radGridView.RowCount < 1)
            {
                MessageBox.Show("没有可以导出的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            var filter = "Excel (*.xls)|*.xls";

            if (exportFormat == ExportFormat.EXCEL)
            {
                filter = "Excel (*.xls)|*.xls";
                var path = FileSelect.SaveAs(filter, "C:\\");
                if (path == "")
                {
                    return;
                }
                GridViewExport.RunExportToExcelML(path, radGridView);
            }
            else if (exportFormat == ExportFormat.HTML)
            {
                filter = "Html File (*.htm)|*.htm";
                var path = FileSelect.SaveAs(filter, "C:\\");
                if (path == "")
                {
                    return;
                }
                GridViewExport.RunExportToHTML(path, radGridView);
            }
            else if (exportFormat == ExportFormat.PDF)
            {
                filter = "PDF file (*.pdf)|*.pdf";
                var path = FileSelect.SaveAs(filter, "C:\\");
                if (path == "")
                {
                    return;
                }
                GridViewExport.RunExportToPDF(path, radGridView);
            }
            else if (exportFormat == ExportFormat.CSV)
            {
                filter = "CSV file (*.csv)|*.csv";
                var path = FileSelect.SaveAs(filter, "C:\\");
                if (path == "")
                {
                    return;
                }
                GridViewExport.RunExportToCSV(path, radGridView);
            }
        }
Beispiel #3
0
        public static int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten, string fileName, bool IsShowExcel)
        {
            FileStream fs       = null;
            int        i        = 0;
            int        j        = 0;
            int        count    = 0;
            ISheet     sheet    = null;
            IWorkbook  workbook = null;

            if (null == data)
            {
                return(0);
            }
            if (data.Rows.Count <= 0)
            {
                return(0);
            }
            if (fileName == "")
            {
                fileName = FileSelect.SaveAs("Microsoft Excel files(*.xls)|*.xls", "C:\\");
            }
            if (fileName == "")
            {
                return(0);
            }

            try
            {
                fs = new FileStream(fileName, FileMode.Create);
            }
            catch (Exception ex)
            {
                MessageBox.Show("创建文件失败!请检查文件是否被占用,关闭已打开文件后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(0);
            }

            try
            {
                if (Path.GetExtension(fileName) == ".xlsx") // 2007版本
                {
                    workbook = new XSSFWorkbook();
                }
                else if (Path.GetExtension(fileName) == ".xls") // 2003版本
                {
                    fs.Position = 0;
                    workbook    = new HSSFWorkbook();
                }
            }
            catch (Exception ex)
            {
                var exMessage = "";
                if (ex.Message == "" && ex.InnerException != null)
                {
                    exMessage = ex.InnerException.Message;
                }
                MessageBox.Show(exMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(0);
            }

            if (workbook != null)
            {
                if (sheetName.Trim() == "")
                {
                    sheetName = "Sheet1";
                }
                sheet = workbook.CreateSheet(sheetName);
            }
            else
            {
                MessageBox.Show("创建Sheet失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(-1);
            }

            if (isColumnWritten) //写入DataTable的列名
            {
                IRow row = sheet.CreateRow(0);
                for (j = 0; j < data.Columns.Count; ++j)
                {
                    row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                }
                count = 1;
            }
            else
            {
                count = 0;
            }
            try
            {
                for (i = 0; i < data.Rows.Count; ++i)
                {
                    IRow row = sheet.CreateRow(count);
                    for (j = 0; j < data.Columns.Count; ++j)
                    {
                        row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                    }
                    ++count;
                }
                workbook.Write(fs); //写入到excel
                if (IsShowExcel)
                {
                    DialogResult dr = MessageBox.Show("The data in the grid was exported successfully. Do you want to open the file?",
                                                      "Export to Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        if (File.Exists(fileName))
                        {
                            System.Diagnostics.Process.Start(fileName);
                        }
                    }
                }
                CloseStream(fs);
                return(count);
            }
            catch (Exception ex)
            {
                CloseStream(fs);
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(-1);
            }
        }
        /// <summary>
        /// 将DataTable数据导入到excel中
        /// </summary>
        /// <param name="data">要导入的数据</param>
        /// <param name="isColumnWritten">DataTable的列名是否要导入</param>
        /// <param name="sheetName">要导入的excel的sheet的名称</param>
        /// <returns>导入数据行数(包含列名那一行)</returns>
        public static int GridViewToExcel(RadGridView radGridView, string sheetName, bool isColumnWritten, bool IsIncludeFirstCol)
        {
            int    i     = 0;
            int    j     = 0;
            int    count = 0;
            ISheet sheet = null;

            var data = RadGridViewHelper.ConvertGridViewToDataTable(radGridView, IsIncludeFirstCol);

            if (null == data || data.Rows.Count <= 0)
            {
                return(0);
            }
            var fileName = FileSelect.SaveAs("Microsoft Excel files(*.xls)|*.xls", "C:\\");

            if (fileName == "")
            {
                return(0);
            }
            FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            if (fileName.IndexOf(".xlsx") > 0) // 2007版本
            {
                workbook = new XSSFWorkbook();
            }
            else if (fileName.IndexOf(".xls") > 0) // 2003版本
            {
                workbook = new HSSFWorkbook();
            }

            try
            {
                if (workbook != null)
                {
                    sheet = workbook.CreateSheet(sheetName);
                }
                else
                {
                    return(-1);
                }

                if (isColumnWritten == true) //写入DataTable的列名
                {
                    IRow row = sheet.CreateRow(0);
                    for (j = 0; j < data.Columns.Count; ++j)
                    {
                        row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                    }
                    count = 1;
                }
                else
                {
                    count = 0;
                }

                for (i = 0; i < data.Rows.Count; ++i)
                {
                    IRow row = sheet.CreateRow(count);
                    for (j = 0; j < data.Columns.Count; ++j)
                    {
                        row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                    }
                    ++count;
                }
                workbook.Write(fs); //写入到excel
                RadMessageBox.SetThemeName(radGridView.ThemeName);
                DialogResult dr = RadMessageBox.Show("The data in the grid was exported successfully. Do you want to open the file?",
                                                     "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(fileName);
                    }
                    catch (Exception ex)
                    {
                        string message = String.Format("The file cannot be opened on your system.\nError message: {0}", ex.Message);
                        RadMessageBox.Show(message, "Open File", MessageBoxButtons.OK, RadMessageIcon.Error);
                    }
                }
                if (fs != null)
                {
                    fs.Close();
                }
                return(count);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                return(-1);
            }
        }