예제 #1
0
        public bool ReadTextFile(string strFile)
        {
            //读取文件
            try
            {
                string       strText = "";
                StreamReader sr      = new StreamReader(strFile, System.Text.Encoding.Default);
                string       strTemp = "";
                count = 0;
                while (sr.Peek() >= 0)
                {
                    strTemp = sr.ReadLine();
                    //读取文本
                    strText += strTemp + "\n";
                    count++;
                }
                sr.Close();

                PointInformationList = new List <CPointInformation>(count);
                for (int i = 0; i < count; i++)
                {
                    //读取文本
                    string[] strSplitLine;
                    string[] strOri;

                    strSplitLine = System.Text.RegularExpressions.Regex.Split(strText.Trim(), @"\n");
                    CPointInformation pointinfo = new CPointInformation();
                    if (strSplitLine.Length > 0)
                    {
                        strOri            = System.Text.RegularExpressions.Regex.Split(strSplitLine[i], @" ");
                        pointinfo.ID      = int.Parse(strOri[0]);
                        pointinfo.PointID = int.Parse(strOri[1]);
                        pointinfo.X       = double.Parse(strOri[2]);
                        pointinfo.Y       = double.Parse(strOri[3]);
                        pointinfo.Z       = double.Parse(strOri[4]);
                        pointinfo.omg     = double.Parse(strOri[5]);
                        pointinfo.phi     = double.Parse(strOri[6]);
                        pointinfo.kap     = double.Parse(strOri[7]);
                        pointinfo.width   = int.Parse(strOri[8]);
                        pointinfo.height  = int.Parse(strOri[9]);
                        pointinfo.fx      = int.Parse(strOri[10]);
                        pointinfo.fy      = int.Parse(strOri[11]);
                        pointinfo.f       = double.Parse(strOri[12]);

                        PointInformationList.Add(pointinfo);
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return(false);
            }
            return(true);
        }
예제 #2
0
        public bool ReadExcel(string str)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                //打开目标文件
                Workbook wbook = ExcelApp.Workbooks.Open(str, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                //设置第一个工作薄
                Worksheet wsheet = (Microsoft.Office.Interop.Excel.Worksheet)wbook.Worksheets[1];
                //获得使用的行数、列数
                int rowCount = wsheet.UsedRange.CurrentRegion.Rows.Count;
                if (rowCount <= 1)
                {
                    System.Windows.Forms.MessageBox.Show("文件中没有数据记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                count = rowCount - 1;
                PointInformationList = new List <CPointInformation>(rowCount - 1);
                int colCount = wsheet.UsedRange.CurrentRegion.Columns.Count;
                if (colCount < 13)
                {
                    System.Windows.Forms.MessageBox.Show("字段个数不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                object[,] sheetdata = wsheet.UsedRange.get_Value(Type.Missing) as object[, ];

                //定义字段索引
                int IDindex      = -1;
                int PointIDindex = -1;
                int Xindex       = -1;
                int Yindex       = -1;
                int Zindex       = -1;
                int omgindex     = -1;
                int phiindex     = -1;
                int kapindex     = -1;
                int widthindex   = -1;
                int heightindex  = -1;
                int fxindex      = -1;
                int fyindex      = -1;
                int findex       = -1;
                for (int i = 1; i <= colCount; i++)
                {
                    switch (sheetdata[1, i].ToString())
                    {
                    case "ID":
                        IDindex = i;
                        break;

                    case "PointID":
                        PointIDindex = i;
                        break;

                    case "X":
                        Xindex = i;
                        break;

                    case "Y":
                        Yindex = i;
                        break;

                    case "Z":
                        Zindex = i;
                        break;

                    case "omg":
                        omgindex = i;
                        break;

                    case "phi":
                        phiindex = i;
                        break;

                    case "kap":
                        kapindex = i;
                        break;

                    case "width":
                        widthindex = i;
                        break;

                    case "height":
                        heightindex = i;
                        break;

                    case "fx":
                        fxindex = i;
                        break;

                    case "fy":
                        fyindex = i;
                        break;

                    case "f":
                        findex = i;
                        break;

                    default:
                        break;
                    }
                }
                if (IDindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("ID字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (PointIDindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("PointID字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (Xindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("X字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (Yindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("Y字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (Zindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("Z字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (omgindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("omg字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (phiindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("phi字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (kapindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("kap字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (widthindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("width字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (heightindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("height字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (fxindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("fx字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (fyindex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("fy字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                if (findex == -1)
                {
                    System.Windows.Forms.MessageBox.Show("f字段缺失", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }
                for (int j = 2; j <= rowCount; j++)
                {
                    CPointInformation pointinfo = new CPointInformation();
                    if (sheetdata[j, IDindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行ID字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (int.TryParse(sheetdata[j, IDindex].ToString(), out pointinfo.ID) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行ID字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, PointIDindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行PointID字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (int.TryParse(sheetdata[j, PointIDindex].ToString(), out pointinfo.PointID) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行PointID字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, Xindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行X字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (double.TryParse(sheetdata[j, Xindex].ToString(), out pointinfo.X) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行X字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, Yindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行Y字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (double.TryParse(sheetdata[j, Yindex].ToString(), out pointinfo.Y) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行Y字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, Zindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行Z字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (double.TryParse(sheetdata[j, Zindex].ToString(), out pointinfo.Z) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行Z字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, omgindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行omg字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (double.TryParse(sheetdata[j, omgindex].ToString(), out pointinfo.omg) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行omg字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, phiindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行phi字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (double.TryParse(sheetdata[j, phiindex].ToString(), out pointinfo.phi) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行phi字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, kapindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行ka字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (double.TryParse(sheetdata[j, kapindex].ToString(), out pointinfo.kap) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行ka字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, widthindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行width字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (int.TryParse(sheetdata[j, widthindex].ToString(), out pointinfo.width) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行width字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, heightindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行height字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (int.TryParse(sheetdata[j, heightindex].ToString(), out pointinfo.height) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行height字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, fxindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行fx字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (int.TryParse(sheetdata[j, fxindex].ToString(), out pointinfo.fx) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行fx字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, fyindex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行fy字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (int.TryParse(sheetdata[j, fyindex].ToString(), out pointinfo.fy) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行fy字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (sheetdata[j, findex] == null)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行f字段是否为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    if (double.TryParse(sheetdata[j, findex].ToString(), out pointinfo.f) == false)
                    {
                        System.Windows.Forms.MessageBox.Show("请检查第" + j.ToString() + "行f字段是否为整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(false);
                    }
                    PointInformationList.Add(pointinfo);
                }
                ExcelApp.Quit();
                ExcelApp = null;
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }