How to set a number string into a real string formation? - npoi

I'm now using NPOI to cope with Excel export, and here's my codes (part in .NET):
int rowIndex = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
for (int j = 0; j < cellCount; j++)
{
cell = dataRow.CreateCell(j,CellType.String);
cell.SetCellValue(new HSSFRichTextString(dt.Rows[i][j].ToString()));
}
rowIndex++;
}
What makes me feel surprised is there's a list whose number string is "20150525", and it will be analyzed as "2015……E+10" formation (scientific number formation). However I wanna keep it as a string value. So How?
Thanks!

In fact we have to set a CellStyle, snippet of sample codes is below:
IRow row = book[0].CreateRow(rowIndex + 1);
ICell rowCell = null;
rowCell = row.CreateCell(colIndex);
rowCell.SetCellValue(realCellValue);
ICellStyle cellStyle = book.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#");
rowCell.CellStyle = cellStyle;

Related

Sort method c# string to char

I'm trying to do Sort method, however i get this error:
IndexOutOfRangeException, on the line if(chars[i] > chars1[y]). Amount is equal to 25
string temp1;
for (int i = 0; i < amount; i++)
{
for (int y = i + 1; y < amount - 1; y++)
{
var chars = Duomenys[i].Pozicija.ToCharArray();
var chars1 = Duomenys[y].Pozicija.ToCharArray();
if (chars[i] > chars1[y])
{............}
You are using the same indices (i and y) to identify locations within the array Duomenys and chars/chars1, which seem like very different things. Can't tell what you should be doing instead, given the lack of information provided.

Create Random Number List With No Repetition

I'm looking to create a list of 'random' numbers from 1 to 15 but without any repetition. I have created an array and looking to store each number in it but can't figure out how to do this. I've gotten as far as creating the random list and storing them in the array but can't quite get to ensure there are no repetitions. Any help would be appreciated. My code is as follows:
int[] myList = new int[15];
Random random = new Random();
for (int i = 0; myList.Length; i++)
{
myList[i] = random.Next(1, 15);
}
Because the size of your list is equal to the possible values, you can just create the list in normal order:
int[] myList = new int[15];
for (int i = 0; i < myList.Length; i++)
{
myList[i] = i + 1;
}
and then shuffle it, for example by assigning a random value to each entry and sort by that value:
Random random = new Random();
myList = myList.OrderBy(a => random.Next()).ToArray();
You can do it using Fisher–Yates shuffle.
Sample Implementation:
int n = 15;
int[] myList = new int[n];
Random random = new Random();
for (int i = 0; i < n; i++)
{
myList[i] = i + 1;
}
for (int i = n - 1; i >= 1; i--)
{
int j = random.Next(1, i);
int temp=myList[i];
myList[i]=myList[j];
myList[j]=temp;
}
You need to get the algorithm right.
Start from i=15
Pick a random number from 1 to i.
Append it to the list.
Swap it with (i-1)th index.
Decrement i by 1.
Repeat the above steps.
The code for above can be:
int[] myList = new int[15];
int[] original_list = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Random random = new Random();
for (int i = myList.Length; i>=0; i--)
{int randNo = random.Next(0, i-1);
myList[i] = original_list[randNo];
swap(original_list[i-1],original_list[randNo]); //your swap method
}

Most efficient way to search through rows and change values in columns of a large array (50,000 by 50,000)

Say there is a large two dimensional array (50,000 by 50,000). Essentially all the elements in the array are 1s and 0s. I am try to figure out an efficient way to change values in columns (1s to 0s and 0s to 1s). But more importantly see which rows have matching elements (a row with all the same values).
I suppose you could do something like
int[][] data = new int[1000][1000];
// populate
final Random random = new Random();
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
final int value = random.nextInt();
data[i][j] = ((value % 2 == 0) ? 1 : 0);
}
}
final List<BigInteger> bitStrings = new ArrayList<>();
StringBuilder builder = null;
for (int i = 0; i < data.length; i++) {
builder = new StringBuilder();
for (int j = 0; j < data[i].length; j++) {
final int value = data[i][j];
builder.append(value);
}
final BigInteger bitString = new BigInteger(builder.toString(), 2);
bitStrings.add(bitString);
}
for (final BigInteger bitString : bitStrings) {
System.out.println(bitString.toString(2));
}
And use the BigInteger operations for bitwise operations.

actionscript 2: pick 3 random nummers from array

i need 3 random variables from array 0-36 (roulette).
i have this code. script return first 3 nummers from random array.
i need return 3 random nummers (from random position) from random array.
help me please.
onClipEvent (load) {
// field 0-36
var rands = [];
for (var i = 0; i < 36; ++i)
{
rands[i] = i;
}
// random sorting array
rands.sort(function(a,b) {return random(3)-1;});
// final variables 1,2,3
random1 = rands[0];
random2 = rands[1];
random3 = rands[2];
}
this is possible code for 1 variable, i need convert this to 3 variables in AS2
n = 3;
for (var i:Number = 0; i < n; i++) {
var randomSelection = Math.floor((Math.random() * rands.length));
trace("Selected: " + rands[randomSelection]);
}
It's not 100% clear from your question what you want, but this is my guess:
var n = 3;
var random = [];
for (var i:Number = 0; i < n; i++) {
// Store the random selections in an array
random.push(Math.floor(Math.random() * rands.length));
}
// You could assign them to variables or access directly via array
var random1 = random[0];
var random2 = random[1];
var random3 = random[2];

add custom clickable buttons

I am developing an app for win phone 7. In that I need to add clickable buttons(not specific number) into a grid dynamically.and navigate the page with some information to other page.
can any body help me please....
private void buildThumbs(Grid gridThumb, int p) {
int n;
if (p % 3 == 0)
n = p / 3;
else
n = (p / 3) + 1;
GridLength height = new GridLength(100);
for (int i = 0; i < n; i++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.MinHeight = 100;
gridThumb.RowDefinitions.Add(rowDef);
}
MovieThumb[,] thumb = new MovieThumb[n, 3];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 3; j++)
{
Image ni=new Image();
ImageSourceConverter ims = new ImageSourceConverter();
ni.SetValue(Image.SourceProperty ,ims.ConvertFromString( "/Images/book1.png"));
thumb[i, j].SetValue(Grid.ColumnProperty, j);
thumb[i,j].SetValue(Grid.RowProperty,i);
thumb.Click += new RoutedEventHandler(thumb_click("sandy"));
thumb[i, j].CoverImage = ni;
thumb[i, j].Loading = "Loading";
thumb[i, j].progress = false;
gridThumb.Children.Add(thumb);
}
}
}
I am not quite sure if I understand your problem, but if you want to set the Column and Row number for your dynamically created buttons, you may do so with Grid.SetColumn(buttonname, column); Grid.SetRow(buttonname, row); before you add the object to the gridThumb.
We need some more information for what exactly you want to accomplish and what doesn't work for you to help :)

Resources