我需要代码生成一个随机电话号码,如果它选择了331前缀,那么它必须有2x运营商号码,如果它选择333那么它必须包含5x运营商号码,最后随机数字,但如果它是381那么它是7位数字,如果它是389它是一个6位数字我已经这样做了,但最后它没有打印出任何东西。
public static void LargeExport(string filename)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
{
//this list of attributes will be used when writing a start element
List<OpenXmlAttribute> attributes;
OpenXmlWriter writer;
document.AddWorkbookPart();
WorksheetPart workSheetPart = document.WorkbookPart.AddNewPart<WorksheetPart>();
writer = OpenXmlWriter.Create(workSheetPart);
writer.WriteStartElement(new Worksheet());
writer.WriteStartElement(new SheetData());
for (int rowNum = 1; rowNum <= 115000; ++rowNum)
{
//create a new list of attributes
attributes = new List<OpenXmlAttribute>();
// add the row index attribute to the list
attributes.Add(new OpenXmlAttribute("r", null, rowNum.ToString()));
//write the row start element with the row index attribute
writer.WriteStartElement(new Row(), attributes);
for (int columnNum = 1; columnNum <= 30; ++columnNum)
{
//reset the list of attributes
attributes = new List<OpenXmlAttribute>();
// add data type attribute - in this case inline string (you might want to look at the shared strings table)
attributes.Add(new OpenXmlAttribute("t", null, "str"));
//add the cell reference attribute
attributes.Add(new OpenXmlAttribute("r", "", string.Format("{0}{1}", GetColumnName(columnNum), rowNum)));
//write the cell start element with the type and reference attributes
writer.WriteStartElement(new Cell(), attributes);
//write the cell value
writer.WriteElement(new CellValue(string.Format("This is Row {0}, Cell {1}", rowNum, columnNum)));
// write the end cell element
writer.WriteEndElement();
}
// write the end row element
writer.WriteEndElement();
}
// write the end SheetData element
writer.WriteEndElement();
// write the end Worksheet element
writer.WriteEndElement();
writer.Close();
writer = OpenXmlWriter.Create(document.WorkbookPart);
writer.WriteStartElement(new Workbook());
writer.WriteStartElement(new Sheets());
writer.WriteElement(new Sheet()
{
Name = "Large Sheet",
SheetId = 1,
Id = document.WorkbookPart.GetIdOfPart(workSheetPart)
});
// End Sheets
writer.WriteEndElement();
// End Workbook
writer.WriteEndElement();
writer.Close();
document.Close();
}
}
//A simple helper to get the column name from the column index. This is not well tested!
private static string GetColumnName(int columnIndex)
{
int dividend = columnIndex;
string columnName = String.Empty;
int modifier;
while (dividend > 0)
{
modifier = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modifier).ToString() + columnName;
dividend = (int)((dividend - modifier) / 26);
}
return columnName;
}
答案 0 :(得分:2)
你有几个问题。首先,如果您在之前重置数组,则回显它们,每次都会得到相同的结果。
接下来,您需要针对字符串测试数组第一个位置中元素的key,例如'a'
,而不是['a']
,因为第二个示例永远不会匹配,除非你的密钥有括号。
最后,您必须在已经洗牌的任何后续数组的第一个位置返回该元素。您有两个名称相同的数组$string_prefix
- 第二个数组覆盖第一个数组。您需要额外的逻辑来做出决定:
//an array containing the prefixes
$string_preprefix = array('a' => '331', 'b' => '333',);
shuffle($string_preprefix);
//reset($string_preprefix);
if('389' == $string_preprefix[0]) { // this value is not in the original array
//an array containing the operator numbers for the 389 prefix
$string_prefix = array('20', '22', '23', '24', '26', '28', '29',);
shuffle($string_prefix);
//reset($string_prefix);
} elseif ('381' == $string_preprefix[0]) { // this value is not in the original array
//an array containing the operator numbers for the 381 prefix
$string_prefix = array('53', '54', '55', );
shuffle($string_prefix);
//reset($string_prefix);
} else {
$string_prefix = '0000';
}
//a function that generates random number
function generateRandomNumber($length = 6) {
$number = '1234567890';
$numberLength = strlen($number);
$randomNumber = '';
for ($i = 0; $i < $length; $i++) {
$randomNumber .= $number[rand(0, $numberLength - 1)];
}
return $randomNumber;
}
//the if/elseif method doesn't print anything
if (key($string_preprefix) == 'a'):
echo $string_preprefix[0] . $string_prefix[0] . generateRandomNumber(6);
elseif(key($string_preprefix) == 'b'):
echo $string_preprefix[0] . $string_prefix[0] . generateRandomNumber(7);
endif;
此外,我同意@ IanDrake的评论 - shuffle()
可能不会做你想要的。您将要执行数组的随机化。