Beispiel #1
0
        bool CreateIniXML(string strFile)
        {
            try
            {
                string strCurPath = Directory.GetCurrentDirectory();
                // 打开excel
                FileStream   file = new FileStream(strCurPath + "/" + strFile, FileMode.Open, FileAccess.Read);
                XSSFWorkbook wb   = new XSSFWorkbook(file);
                ////////////////////////////////////////////////////////////////////////////
                XmlDocument    iniDoc = new XmlDocument();
                XmlDeclaration xmlDecl;
                xmlDecl = iniDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                iniDoc.AppendChild(xmlDecl);

                XmlElement root = iniDoc.CreateElement("", "XML", "");
                iniDoc.AppendChild(root);

                // 读取excel中每一个sheet
                foreach (XSSFSheet sheet in wb)
                {
                    string strSheetName = sheet.SheetName;
                    int    nRowCount    = sheet.LastRowNum;
                    int    nColCount    = sheet.GetRow(0).LastCellNum;

                    System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
                    string[] colNames = new string[nColCount];
                    rows.MoveNext();
                    bool isEmpty = false;
                    for (int i = 0; i < nColCount; i++)
                    {
                        try
                        {
                            if (((XSSFRow)rows.Current).GetCell(i).StringCellValue != "")
                            {
                                colNames[i] = ((XSSFRow)rows.Current).GetCell(i).StringCellValue;
                            }
                            else
                            {
                                isEmpty = true;
                                break;
                            }
                        }
                        catch (Exception err)
                        {
                            isEmpty = true;
                            break;
                        }
                    }
                    if (isEmpty)
                    {
                        break;
                    }
                    while (rows.MoveNext())
                    {
                        XSSFRow row       = rows.Current as XSSFRow;
                        string  testValue = "";
                        try
                        {
                            testValue = row.GetCell(0).StringCellValue;
                        }
                        catch (Exception err)
                        {
                            continue;
                        }

                        if (testValue.IsNullOrEmpty())
                        {
                            continue;
                        }
                        XmlElement objectNode = iniDoc.CreateElement("", "Object", "");
                        root.AppendChild(objectNode);
                        for (Int32 col = 0; col < nColCount; ++col)
                        {
                            try
                            {
                                string name = colNames[col];

                                string value = "";
                                if (!row.GetCell(col).IsNull())
                                {
                                    var valueCell = row.GetCell(col);
                                    if (valueCell.CellType == NPOI.SS.UserModel.CellType.Boolean)
                                    {
                                        value = row.GetCell(col).BooleanCellValue ? "1" : "0";
                                    }
                                    else if (valueCell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                                    {
                                        value = row.GetCell(col).NumericCellValue.ToString();
                                    }
                                    else
                                    {
                                        value = row.GetCell(col).StringCellValue;
                                    }
                                }
                                objectNode.SetAttribute(name, value.ToString());
                            }
                            catch (Exception err)
                            {
                                MessageBox.Show(strFile + "\n" + "Sheet: " + strSheetName + "\n" + "Cell: " + row.ToString() + ", " + col.ToString() + "\n" + "This cell is empty or error\n" + err.Message);
                                return(false);
                            }
                        }
                    }
                }

                ////////////////////////////////////////////////////////////////////////////
                // 保存文件
                int    nLastPoint  = strFile.LastIndexOf(".") + 1;
                int    nLastSlash  = strFile.LastIndexOf("/") + 1;
                string strFileName = strFile.Substring(nLastSlash, nLastPoint - nLastSlash - 1);
                string strFileExt  = strFile.Substring(nLastPoint, strFile.Length - nLastPoint);

                string strXMLFile = strToolBasePath + strXMLIniPath + strFileName;
                if (nCipher > 0)
                {
                    strXMLFile += ".NF";
                }
                else
                {
                    strXMLFile += ".xml";
                }

                //CloseFile(strXMLFile);
                iniDoc.Save(strXMLFile);

                ProcessEncryptFile(strXMLFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Some exception is found, File={0}, error:{1}", strFile, ex.Message));
                Console.WriteLine("Some exception is found, File={0}, error:{1}", strFile, ex.Message);
                return(false);
            }

            return(true);
        }