Exemple #1
0
        /// <summary>
        /// 创建xml文件
        /// </summary>
        /// <param name="_url">excel 文件路径</param>
        /// <returns></returns>
        public bool CreateXML(string _url)
        {
            try
            {
                //创建并复制模板文件
                if (System.IO.File.Exists(_filename))
                {
                    System.IO.File.Delete(_filename);
                }
                System.IO.File.Copy(_path + "data_template.xml", _filename);
                //读取excel表格内容
                HSSFWorkbook workbook  = HSSFTestData.OpenSampleWorkbook(_url);
                HSSFSheet    shtClient = workbook.GetSheetAt(0);//名录表

                int count = shtClient.LastRowNum;
                if (count == 0)
                {
                    return(false);
                }
                System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
                xmldoc.Load(_filename);
                //生成文件标题
                int     cellnum        = shtClient.GetRow(0).LastCellNum;
                XmlNode xnodeFileField = xmldoc.SelectSingleNode(@"root/FileField");
                for (int i = 0; i < cellnum; i++)
                {
                    XmlElement el = xmldoc.CreateElement("Field");
                    el.SetAttribute("name", "Field" + i);
                    el.SetAttribute("value", GetCellValue(shtClient.GetRow(0).GetCell(i)));
                    xnodeFileField.AppendChild(el);
                }
                //读取内容
                count = count > 1000 ? 1000 : count;
                XmlNode xnodeList = xmldoc.SelectSingleNode(@"root/List");
                for (int i = 1; i <= count; ++i)//从第2行开始
                {
                    if (ISCheckRowStrNullOrEmpty(i, shtClient))
                    {
                        continue;
                    }
                    XmlElement xlItem = xmldoc.CreateElement("Item");
                    xlItem.SetAttribute("id", i.ToString());
                    xnodeList.AppendChild(xlItem);
                    for (int j = 0; j < cellnum; ++j)//读取每格数据
                    {
                        XmlElement xlData = xmldoc.CreateElement("Data");
                        xlData.SetAttribute("name", "Field" + j.ToString());
                        xlData.InnerText = GetCellValue(shtClient.GetRow(i).GetCell(j));
                        xlItem.AppendChild(xlData);
                    }
                }
                xmldoc.Save(_filename);
                return(true);
            }
            catch (Exception mye)
            {
                string str = mye.Message;
            }
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// 名录数据导入
        /// </summary>
        /// <returns></returns>
        public bool Import()
        {
            if (string.IsNullOrEmpty(_url))
            {
                _result.Add("请选择文件");
                return(false);
            }
            HSSFWorkbook workbook = HSSFTestData.OpenSampleWorkbook(_url);

            if (workbook == null)
            {
                _result.Add("无法读取zip文件,请用户由指定模板进行导入");
                return(false);
            }
            if (workbook.NumberOfSheets < 1)
            {
                _result.Add("无法读取Excel文件,请用户由指定模板进行导入!");
                return(false);
            }
            HSSFSheet shtClient = workbook.GetSheetAt(0);//名录表

            //HSSFSheet shtSource = workbook.GetSheetAt(1);//来源表
            //HSSFSheet shtTrade = workbook.GetSheetAt(2);//行业表
            //HSSFSheet shtArea = workbook.GetSheetAt(3); //地区表

            if (Check(shtClient))
            {
                //---------导入名录信息---------
                return(ImportClient(shtClient));
            }
            return(false);
        }