本文整理汇总了C#中NPOI.HSSF.UserModel.HSSFWorkbook.CreateCellStyle方法的典型用法代码示例。如果您正苦于以下问题:C# HSSFWorkbook.CreateCellStyle方法的具体用法?C# HSSFWorkbook.CreateCellStyle怎么用?C# HSSFWorkbook.CreateCellStyle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPOI.HSSF.UserModel.HSSFWorkbook
的用法示例。
在下文中一共展示了HSSFWorkbook.CreateCellStyle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddDateRangetoExcelSheet
public void AddDateRangetoExcelSheet(HSSFWorkbook workbook, ISheet sheet)
{
//Create a Title row
var titleFont = workbook.CreateFont();
titleFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
titleFont.FontHeightInPoints = 11;
titleFont.Underline = NPOI.SS.UserModel.FontUnderlineType.Single;
var titleStyle = workbook.CreateCellStyle();
titleStyle.SetFont(titleFont);
var row = sheet.CreateRow(rowCount++);
var cell = row.CreateCell(0);
cell.CellStyle = titleStyle;
cell.SetCellValue("Date Range");
row = sheet.CreateRow(rowCount++);
cell = row.CreateCell(0);
var value = string.Format("Start Date: {0}", StartDate.ToString("MM-dd-yyyy"));
cell.SetCellValue(value);
row = sheet.CreateRow(rowCount++);
cell = row.CreateCell(0);
value = string.Format("End Date: {0}", EndDate.ToString("MM-dd-yyyy"));
cell.SetCellValue(value);
}
示例2: TestDoesNoHarmIfNothingToDo
public void TestDoesNoHarmIfNothingToDo()
{
HSSFWorkbook wb = new HSSFWorkbook();
IFont f = wb.CreateFont();
f.FontName = ("Testing");
NPOI.SS.UserModel.ICellStyle s = wb.CreateCellStyle();
s.SetFont(f);
Assert.AreEqual(5, wb.NumberOfFonts);
Assert.AreEqual(22, wb.NumCellStyles);
// Optimise fonts
HSSFOptimiser.OptimiseFonts(wb);
Assert.AreEqual(5, wb.NumberOfFonts);
Assert.AreEqual(22, wb.NumCellStyles);
Assert.AreEqual(f, s.GetFont(wb));
// Optimise styles
HSSFOptimiser.OptimiseCellStyles(wb);
Assert.AreEqual(5, wb.NumberOfFonts);
Assert.AreEqual(22, wb.NumCellStyles);
Assert.AreEqual(f, s.GetFont(wb));
}
示例3: CreateStyles
public static StyleContainer CreateStyles(HSSFWorkbook workbook)
{
var styles = new StyleContainer();
var h1Font = workbook.CreateFont();
h1Font.FontHeightInPoints = (short)24;
h1Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
var h2Font = workbook.CreateFont();
h2Font.FontHeightInPoints = (short)16;
h2Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
var h3Font = workbook.CreateFont();
h3Font.FontHeightInPoints = (short)12;
h3Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
var titleFont = workbook.CreateFont();
titleFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
styles.RightAligned = workbook.CreateCellStyle();
styles.RightAligned.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
styles.Date = workbook.CreateCellStyle();
styles.Date.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
styles.Date.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");
styles.Currency = workbook.CreateCellStyle();
styles.Currency.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
//styles.Currency.DataFormat = HSSFDataFormat.GetBuiltinFormat("$#,##0.00");
styles.Currency.DataFormat = (short)7;
styles.LeftAligned = workbook.CreateCellStyle();
styles.LeftAligned.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
styles.TitleStyle = workbook.CreateCellStyle();
styles.TitleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
styles.TitleStyle.SetFont(titleFont);
// Fonts are set into a style so create a new one to use.
styles.HeaderStyle = workbook.CreateCellStyle();
styles.HeaderStyle.SetFont(h1Font);
styles.HeaderStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
// Fonts are set into a style so create a new one to use.
styles.Header2Style = workbook.CreateCellStyle();
styles.Header2Style.SetFont(h2Font);
// Fonts are set into a style so create a new one to use.
styles.Header3Style = workbook.CreateCellStyle();
styles.Header3Style.SetFont(h3Font);
return styles;
}
示例4: AddBorder
/// <summary>
/// 加边框
/// </summary>
/// <param Name="rowindex">1开始</param>
/// <param Name="cellIndex">1开始</param>
public void AddBorder( ISheet sheet, HSSFWorkbook workbook)
{
ICellStyle styel = workbook.CreateCellStyle();
styel.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; // ------------------
IFont font1 = workbook.CreateFont();
font1.FontHeightInPoints = 11;
font1.Boldweight = 600;
font1.FontName = "宋体";
styel.SetFont(font1);
for (int rowindex=1;rowindex<sheet.LastRowNum+1;rowindex++)
{
for (int cellIndex =0; cellIndex < dcs.Count;cellIndex++ )
{
sheet.GetRow(rowindex).RowStyle = styel;
ICell cell = sheet.GetRow(rowindex ).GetCell(cellIndex );
HSSFCellStyle Style = workbook.CreateCellStyle() as HSSFCellStyle;
Style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
Style.VerticalAlignment = VerticalAlignment.Center;
Style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
Style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
Style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
Style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
Style.DataFormat = 0;
Style.SetFont(font1);
cell.CellStyle = Style;
}
}
}
示例5: WorkbookExcelRender
public WorkbookExcelRender()
{
_workbook = new HSSFWorkbook();
_isDisposed = false;
OddCellStyle = _workbook.CreateCellStyle();
EvenCellStyle = _workbook.CreateCellStyle();
OddCellStyleCenterAligned = _workbook.CreateCellStyle();
EvenCellStyleCenterAligned = _workbook.CreateCellStyle();
_nextCellIndex = -1;
RTFHelper = new RTFHelper()
{
NegFont = _workbook.CreateFont(),
NormalFont = _workbook.CreateFont(),
PosFont = _workbook.CreateFont(),
NegFontCrossed = _workbook.CreateFont(),
NormalFontCrossed = _workbook.CreateFont(),
PosFontCrossed = _workbook.CreateFont(),
Pallete = _workbook.GetCustomPalette(),
RTFRenderer = new RtfTextRender()
};
InitDefaultRTFHelper();
InitDefaultCellStyles();
}
示例6: SetCellValue
private static void SetCellValue(HSSFWorkbook workbook, ICell workCell, Type type, dynamic row, Cell cell)
{
var value = type.GetProperty(cell.Field).GetValue(row);
if (value == null)
{
return;
}
if (value is DateTime)
{
workCell.SetCellValue((DateTime)value);
}
else if (value is int)
{
workCell.SetCellValue((int)value);
}
else if (value is double)
{
workCell.SetCellValue((double)value);
}
else
{
workCell.SetCellValue(value.ToString());
}
if (!string.IsNullOrWhiteSpace(cell.Format))
{
var cellStyle = workbook.CreateCellStyle();
var format = workbook.CreateDataFormat();
cellStyle.DataFormat = format.GetFormat(cell.Format);
workCell.CellStyle = cellStyle;
}
}
示例7: TestWriteSheetFont
public void TestWriteSheetFont()
{
HSSFWorkbook wb = new HSSFWorkbook();
NPOI.SS.UserModel.ISheet s = wb.CreateSheet();
IRow r = null;
//ICell c = null;
IFont fnt = wb.CreateFont();
NPOI.SS.UserModel.ICellStyle cs = wb.CreateCellStyle();
fnt.Color=(NPOI.HSSF.Util.HSSFColor.RED.index);
fnt.Boldweight=(short)FontBoldWeight.BOLD;
cs.SetFont(fnt);
for (short rownum = (short)0; rownum < 100; rownum++)
{
r = s.CreateRow(rownum);
r.RowStyle=(cs);
r.CreateCell(0);
}
wb = HSSFTestDataSamples.WriteOutAndReadBack(wb);
SanityChecker sanityChecker = new SanityChecker();
sanityChecker.CheckHSSFWorkbook(wb);
Assert.AreEqual(99, s.LastRowNum, "LAST ROW == 99");
Assert.AreEqual(0, s.FirstRowNum, "FIRST ROW == 0");
}
示例8: btnExport_Click
//数据导出为Excel
private void btnExport_Click(object sender, EventArgs e)
{
string sql = "select CC_AutoId,CC_CustomerName,CC_CellPhone,CC_Landline,CC_BuyDate,CC_CarNum,CC_BracketNum from T_Customers";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, CommandType.Text))
{
if (reader.HasRows)
{
IWorkbook wb = new HSSFWorkbook();
ISheet sheet = wb.CreateSheet("customers");
int rowIndex = 0;//手动处理行索引
while (reader.Read())
{
int autoId = reader.GetInt32(0);
string name = reader.GetString(1);
string cellPhone = reader.GetString(2);
//数据为空,那么就不能Get..,所以要在这里判断
string landLine = reader.IsDBNull(3) ? null : reader.GetString(3);//可为空
DateTime BuyDate = reader.GetDateTime(4);
string carNum = reader.GetString(5);
string bracketNum = reader.GetString(6);
IRow row = sheet.CreateRow(rowIndex);
rowIndex++;
row.CreateCell(0).SetCellValue(autoId);
row.CreateCell(1).SetCellValue(name);
row.CreateCell(2).SetCellValue(cellPhone);
//如果为空或Null,把Excel这个单元格设置为值为空
if (string.IsNullOrEmpty(landLine))
{
row.CreateCell(3).SetCellType(CellType.BLANK);
}
else
{
row.CreateCell(3).SetCellValue(landLine);
}
//============设置日期格式=======
//创建单元格样式对象
ICellStyle cellStyle = wb.CreateCellStyle();
//设置单元格样式对象属性
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");
ICell BuyDateRow = row.CreateCell(4);
BuyDateRow.CellStyle = cellStyle;
BuyDateRow.SetCellValue(BuyDate);
//==============设置完成========
row.CreateCell(5).SetCellValue(carNum);
row.CreateCell(6).SetCellValue(bracketNum);
}
using (FileStream fs = File.OpenWrite("Customers.xls"))
{
wb.Write(fs);
MessageBox.Show("ok");
}
}
}
}
示例9: AddListToExcelSheet
public void AddListToExcelSheet(HSSFWorkbook workbook, ISheet sheet, string Title, Dictionary<string, bool> list)
{
//Create a Title row
var titleFont = workbook.CreateFont();
titleFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
titleFont.FontHeightInPoints = 11;
titleFont.Underline = NPOI.SS.UserModel.FontUnderlineType.Single;
var titleStyle = workbook.CreateCellStyle();
titleStyle.SetFont(titleFont);
var row = sheet.CreateRow(rowCount++);
row = sheet.CreateRow(rowCount++);
var cell = row.CreateCell(0);
cell.CellStyle = titleStyle;
cell.SetCellValue(Title);
foreach (var org in list)
{
if (org.Value == true)
{
row = sheet.CreateRow(rowCount++);
cell = row.CreateCell(0);
cell.SetCellValue(org.Key);
}
}
}
示例10: FillRows
private void FillRows(HSSFWorkbook workbook, ISheet workSheet, List<Cell> cells, dynamic rows)
{
var dateStyle = workbook.CreateCellStyle();
var format = workbook.CreateDataFormat();
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
var currentRow = 1;
if (rows == null)
{
return;
}
foreach (var row in rows)
{
var workRow = workSheet.CreateRow(currentRow);
var currentCol = 0;
var type = row.GetType();
foreach (var cell in cells)
{
var workCell = workRow.CreateCell(currentCol);
SetCellValue(workbook, workCell, type, row, cell);
currentCol++;
}
currentRow++;
}
}
示例11: TestSetAlignment
public void TestSetAlignment()
{
// Create initial cell with default style
IWorkbook wkb = new HSSFWorkbook();
ISheet sheet = wkb.CreateSheet();
IRow row = sheet.CreateRow(0);
ICell cell = row.CreateCell(0);
// Create a new cell with default style
ICell cell2 = row.CreateCell(1);
// Init a new cell style cloned from the one of cell 0
cell2.CellStyle = wkb.CreateCellStyle();
cell2.CellStyle.CloneStyleFrom(cell.CellStyle);
// At this time cell style index should be different
Assert.AreNotEqual(cell.CellStyle.Index, cell2.CellStyle.Index);
// Set an arbitraty cell style property to differentiate the two styles
cell.CellStyle.Alignment = HorizontalAlignment.Right;
// Try to make the same change so that CellUtil will get existing style
CellUtil.SetAlignment(cell2, wkb, (short)HorizontalAlignment.Right);
// Check that cell style has properly been set to HorizontalAlignment.Right
Assert.AreEqual(cell2.CellStyle.Alignment, HorizontalAlignment.Right);
// Check that cell style index are the same again
Assert.AreEqual(cell.CellStyle.Index, cell2.CellStyle.Index);
// Init a new cell style cloned from the one of cell 0
cell2.CellStyle = wkb.CreateCellStyle();
cell2.CellStyle.CloneStyleFrom(cell.CellStyle);
// Set an arbitraty cell style property to differentiate the two styles
cell.CellStyle.Alignment = HorizontalAlignment.Left;
// Try to make different change so that CellUtil will get new style
CellUtil.SetAlignment(cell2, wkb, (short)HorizontalAlignment.Center);
// Check that cell style has alignement property set to HorizontalAlignment.Center
Assert.AreEqual(cell2.CellStyle.Alignment, HorizontalAlignment.Center);
// Check that cell style index are different
Assert.AreNotEqual(cell.CellStyle.Index, cell2.CellStyle.Index);
}
示例12: ExportToExcel
/// <summary>
/// 导出内容到excel下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ExportToExcel()
{
HSSFWorkbook workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet();
//创建一个单元格样式
var backgroundColorStyle = workbook.CreateCellStyle();
backgroundColorStyle.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
backgroundColorStyle.FillForegroundColor = HSSFColor.BLUE.index;
//创建表头行列及赋值样式
var headTitles = new[] { "StudentCode", "ClassCode", "RoomName" };
var headRow = sheet.CreateRow(0);
for (var i = 0; i < 3; i++)
{
var cell = headRow.CreateCell(i);
if (i == 0)
cell.CellStyle = backgroundColorStyle;
cell.SetCellValue(headTitles[i]);
}
//创建数据行列
for (int i = 1; i < this._lessenList.Count + 1; i++)
{
var lesson = this._lessenList[i - 1];
var row = sheet.CreateRow(i);
if (lesson.StudentCode == "BJ02")
row.Height = 50 * 20;
//设置背景色
var cellStudentCode = row.CreateCell(0);
cellStudentCode.CellStyle = backgroundColorStyle;
cellStudentCode.SetCellValue(lesson.StudentCode);
row.CreateCell(1).SetCellValue(lesson.ClassCode);
row.CreateCell(2).SetCellValue(lesson.RoomName);
}
//数据写进内存流
MemoryStream memoryStream = new MemoryStream();
workbook.Write(memoryStream);
//销毁对象
workbook = null;
sheet = null;
headRow = null;
//将内存流以附件形式输出到客户端
var bytes = memoryStream.ToArray();
this._response.AddHeader("Content-Disposition", "attachment;filename=1.xls");
this._response.AddHeader("Content-Length", bytes.Length.ToString());
this._response.OutputStream.Write(bytes, 0, bytes.Length);
this._response.Flush();
}
示例13: CreateBorderCellStyle
public ICellStyle CreateBorderCellStyle(HSSFWorkbook wb)
{
ICellStyle style = wb.CreateCellStyle();
style.Alignment = HorizontalAlignment.Center;
style.VerticalAlignment = VerticalAlignment.Center;
style.BorderTop = BorderStyle.Thin;
style.BorderBottom = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
return style;
}
示例14: btnExportExcel_Click
protected void btnExportExcel_Click(object sender,EventArgs e)
{
HSSFWorkbook workBook = new HSSFWorkbook();
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "杭州农副产品物流网络科技有限公司";
workBook.DocumentSummaryInformation = dsi;
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "采购配送系统配送价格单页";
workBook.SummaryInformation = si;
HSSFSheet sheet = workBook.CreateSheet("价格单页");
DataTable dt = logic.product.listWithPriceByBuyer(companyId);
int rowIndex = 0;
int colIndex = 0;
HSSFRow newRow = null;
HSSFCellStyle style = null;
HSSFCell cell=null;
foreach (DataRow row in dt.Rows)
{
newRow = sheet.CreateRow(rowIndex++);
colIndex = 0;
foreach (DataColumn col in dt.Columns)
{
cell=newRow.CreateCell(colIndex++);
cell.SetCellValue(row[col].ToString());
if (rowIndex % 2 == 1)
{
style = workBook.CreateCellStyle();
style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.GREY_80_PERCENT.index;
style.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
cell.CellStyle = style;
}
}
}
string filename = ddlCompanyList.SelectedItem.Text + ".xls";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)));
Response.Clear();
MemoryStream memoryStream = new MemoryStream();
workBook.Write(memoryStream);
Response.BinaryWrite(memoryStream.GetBuffer());
Response.End();
}
示例15: SetUp
public void SetUp()
{
wb = new HSSFWorkbook();
ISheet sheet = wb.CreateSheet("new sheet");
style = wb.CreateCellStyle();
IDataFormat fmt = wb.CreateDataFormat();
style.DataFormat=(fmt.GetFormat("hh:mm:ss"));
cell11 = sheet.CreateRow(0).CreateCell(0);
form = new DataFormatter();
Evaluator = new HSSFFormulaEvaluator(wb);
}