110,561
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
///将数据转换为Int类型
/// </summary>
public static int ToInt(object data, int defaultValue = default(int))
{
int returnValue = defaultValue;
if (ValidationHelper.IsNullOrEmpty(data) || !int.TryParse(data.ToString(), out returnValue))
returnValue = defaultValue;
return returnValue;
}
/// <summary>
/// 判断字符串是否是int/double
/// </summary>
/// <param name="strNumber">需要判断的字符串</param>
/// <returns></returns>
public static bool IsDouble(string strNumber)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
const string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
const string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
HSSFWorkbook workbookExcel = new HSSFWorkbook();
ISheet sheetExcel = workbookExcel.CreateSheet("Sheet1");
IRow rowExcel = sheetExcel.CreateRow(excelRow);
ICell cellExcel = rowExcel.CreateCell(excelCell);
if (ConvertHelper.ToInt(row[t.DataIndex].ToString(), int.MinValue) != int.MinValue)
{
if (ConvertHelper.ToInt(row[t.DataIndex].ToString(), int.MinValue) <= 99999999999)
cellExcel.SetCellValue(int.Parse(row[t.DataIndex].ToString()));
else //Excel数字大于11个9显示NE+总位数
cellExcel.SetCellValue(row[t.DataIndex].ToString());
}
else if (ConvertHelper.IsDouble(row[t.DataIndex].ToString()))
{
cellExcel.SetCellValue(double.Parse(row[t.DataIndex].ToString()));
}
//else if(日期等其他数据类型){...}
else
{
cellExcel.SetCellValue(row[t.DataIndex].ToString());
}
double doubV = 0;
double.TryParse(drValue, out doubV);
dataRow.CreateCell(j).SetCellValue(doubV);
cell.SetCellType(CellType.Numeric);
这两种都行的
ICell cell = row.CreateCell(0, CellType.Numeric);
switch (dtSource.Columns[oldColumnNames[i]].DataType.ToString())
{
case "System.String"://字符串类型
newCell.SetCellValue(drValue);
break;
case "System.DateTime"://日期类型
DateTime dateV;
DateTime.TryParse(drValue, out dateV);
newCell.SetCellValue(dateV);
newCell.CellStyle = dateStyle;//格式化显示
break;
case "System.Boolean"://布尔型
bool boolV = false;
bool.TryParse(drValue, out boolV);
newCell.SetCellValue(boolV);
break;
case "System.Int16"://整型
case "System.Int32":
case "System.Int64":
case "System.Byte":
int intV = 0;
int.TryParse(drValue, out intV);
newCell.SetCellValue(intV);
break;
case "System.Decimal"://浮点型
case "System.Double":
double doubV = 0;
double.TryParse(drValue, out doubV);
newCell.SetCellValue(doubV);
break;
case "System.DBNull"://空值处理
newCell.SetCellValue("");
break;
default:
newCell.SetCellValue("");
break;
}
看这个例子
//创建工作薄
HSSFWorkbook wk = new HSSFWorkbook();
//创建一个名称为mySheet的表
ISheet tb = wk.CreateSheet("mySheet");
//创建一行,此行为第二行
IRow row = tb.CreateRow(1);
ICellStyle style11 = wk.CreateCellStyle();
style11.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
for (int i = 0; i < 20; i++)
{
ICell cell = row.CreateCell(i); //在第二行中创建单元格
cell.SetCellValue(i);//循环往第二行的单元格中添加数据
cell.CellStyle = style11; //要加上这行,
}
using (FileStream fs = File.OpenWrite(@"c:/myxls.xls")) //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
{
wk.Write(fs); //向打开的这个xls文件中写入mySheet表并保存。
MessageBox.Show("提示:创建成功!");
}