本文整理汇总了C#中NPOI.HSSF.UserModel.HSSFWorkbook.GetSheetIndex方法的典型用法代码示例。如果您正苦于以下问题:C# HSSFWorkbook.GetSheetIndex方法的具体用法?C# HSSFWorkbook.GetSheetIndex怎么用?C# HSSFWorkbook.GetSheetIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPOI.HSSF.UserModel.HSSFWorkbook
的用法示例。
在下文中一共展示了HSSFWorkbook.GetSheetIndex方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NpoiWorksheet
public NpoiWorksheet(HSSFWorkbook book, ISheet sheet) {
sheet.ForceFormulaRecalculation = true;
Book = book;
Sheet = sheet;
Index = book.GetSheetIndex(sheet);
Name = book.GetSheetName(Index);
}
示例2: TestGetSheetIndex
public void TestGetSheetIndex()
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = (HSSFSheet)wb.CreateSheet("Sheet1");
HSSFSheet sheet2 = (HSSFSheet)wb.CreateSheet("Sheet2");
HSSFSheet sheet3 = (HSSFSheet)wb.CreateSheet("Sheet3");
HSSFSheet sheet4 = (HSSFSheet)wb.CreateSheet("Sheet4");
Assert.AreEqual(0, wb.GetSheetIndex(sheet1));
Assert.AreEqual(1, wb.GetSheetIndex(sheet2));
Assert.AreEqual(2, wb.GetSheetIndex(sheet3));
Assert.AreEqual(3, wb.GetSheetIndex(sheet4));
// remove sheets
wb.RemoveSheetAt(0);
wb.RemoveSheetAt(2);
// ensure that sheets are Moved up and Removed sheets are not found any more
Assert.AreEqual(-1, wb.GetSheetIndex(sheet1));
Assert.AreEqual(0, wb.GetSheetIndex(sheet2));
Assert.AreEqual(1, wb.GetSheetIndex(sheet3));
Assert.AreEqual(-1, wb.GetSheetIndex(sheet4));
}
示例3: Template
public ActionResult Template(int? tagId)
{
var basicInfoSheetName = ProUploadService.BASIC_SHEET;
var moreInfoSheetName = ProUploadService.MORE_SHEET;
var supportSheetName = "不要修改";
var headerLabels = new Dictionary<string, dynamic>() {
{"商品代码",new {dataformat=0,width=10}},
{"商品名称",new {dataformat=0,width=20}},
{"描述",new {dataformat=0,width=50}},
{"吊牌价",new {dataformat=2,width=8}},
{"现价",new {dataformat=2,width=8}},
{"品牌名",new {dataformat=0,width=20}},
{ "分类名",new {dataformat=0,width=20}},
{"门店名",new {dataformat=0,width=20}},
{ "促销活动编码",new {dataformat=0,width=20}},
{ "专题编码(多个以,分割)",new {dataformat=0,width=20}},
{"可销售",new {dataformat=2,width=5}},
{"商品货号",new {dataformat=0,width=10}}
};
var workbook = new HSSFWorkbook();
var headerLabelCellStyle = workbook.CreateCellStyle();
headerLabelCellStyle.BorderBottom = BorderStyle.THIN;
headerLabelCellStyle.BorderLeft = BorderStyle.THIN;
headerLabelCellStyle.BorderRight = BorderStyle.THIN;
headerLabelCellStyle.BorderTop = BorderStyle.THIN;
headerLabelCellStyle.WrapText = true;
var headerLabelFont = workbook.CreateFont();
headerLabelFont.Boldweight = (short)FontBoldWeight.BOLD;
headerLabelCellStyle.SetFont(headerLabelFont);
//set support sheet
var supportSheet = workbook.CreateSheet(supportSheetName);
workbook.SetSheetHidden(workbook.GetSheetIndex(supportSheet), true);
Func<int, dynamic, int> supportFill = (rowIndex, data) =>
{
var brandRow = supportSheet.CreateRow(rowIndex++);
var brandCodeCol = brandRow.CreateCell(1);
brandCodeCol.SetCellType(CellType.STRING);
brandCodeCol.SetCellValue(data.Id);
var brandNameCol = brandRow.CreateCell(2);
brandNameCol.SetCellType(CellType.STRING);
brandCodeCol.SetCellValue(data.Name);
return rowIndex;
};
int brandRowIndex = 0;
foreach (var brand in _brandRepo.Get(b => b.Status != (int)DataStatus.Deleted).OrderBy(b => b.Name).Select(b => new { Id = b.Id, Name = b.Name }))
{
brandRowIndex = supportFill(brandRowIndex, brand);
}
int tagRowIndex = brandRowIndex;
var tagLinq = _tagRepo.Get(b => b.Status != (int)DataStatus.Deleted).OrderBy(b => b.Name).Select(b => new { Id = b.Id, Name = b.Name });
//if (tagId.HasValue)
// tagLinq = tagLinq.Where(t => t.Id == tagId.Value);
foreach (var tag in tagLinq.OrderBy(t => t.Name))
{
tagRowIndex = supportFill(tagRowIndex, tag);
}
int storeRowIndex = tagRowIndex;
foreach (var store in _storeRepo.Get(b => b.Status != (int)DataStatus.Deleted).OrderBy(b => b.Name).Select(b => new { Id = b.Id, Name = b.Name }))
{
storeRowIndex = supportFill(storeRowIndex, store);
}
//set basic sheet
var sheet1 = workbook.CreateSheet(basicInfoSheetName);
//workbook.SetSheetOrder(basicInfoSheetName, 0);
var rowFirst = sheet1.CreateRow(0);
Action<int, string, dynamic> cellSetting = (cellindex, desc, option) =>
{
var cell = rowFirst.CreateCell(cellindex);
cell.SetCellType(CellType.STRING);
cell.SetCellValue(desc);
cell.CellStyle = headerLabelCellStyle;
sheet1.SetColumnWidth(cellindex, option.width * 255);
var currentCellStyle = workbook.CreateCellStyle();
currentCellStyle.DataFormat = (short)option.dataformat;
sheet1.SetDefaultColumnStyle(cellindex, currentCellStyle);
};
int index = 0;
foreach (var key in headerLabels.Keys)
{
cellSetting(index++, key, headerLabels[key]);
}
//set constraint
DVConstraint brandConstaint = DVConstraint.CreateFormulaListConstraint(string.Format("'{0}'!$B$1:$B${1}", supportSheetName, brandRowIndex));
CellRangeAddressList brandaddressList = new CellRangeAddressList(1, 1000, 5, 5);
HSSFDataValidation branddataValidation = new HSSFDataValidation(brandaddressList, brandConstaint);
branddataValidation.SuppressDropDownArrow = false;
sheet1.AddValidationData(branddataValidation);
DVConstraint tagConstaint = DVConstraint.CreateFormulaListConstraint(string.Format("'{0}'!$B${1}:$B${2}", supportSheetName, brandRowIndex + 1, tagRowIndex));
CellRangeAddressList tagaddressList = new CellRangeAddressList(1, 1000, 6, 6);
HSSFDataValidation tagdataValidation = new HSSFDataValidation(tagaddressList, tagConstaint);
tagdataValidation.SuppressDropDownArrow = false;
sheet1.AddValidationData(tagdataValidation);
DVConstraint storeConstaint = DVConstraint.CreateFormulaListConstraint(string.Format("'{0}'!$B${1}:$B${2}", supportSheetName, tagRowIndex + 1, storeRowIndex));
//.........这里部分代码省略.........
示例4: TestBackupRecord
public void TestBackupRecord()
{
HSSFWorkbook wb = new HSSFWorkbook();
for (int i = 0; i < 10; i++)
{
HSSFSheet s = (HSSFSheet)wb.CreateSheet("Sheet " + i);
InternalSheet sheet = s.Sheet;
}
// Check the initial order
Assert.AreEqual(0, wb.GetSheetIndex("Sheet 0"));
Assert.AreEqual(1, wb.GetSheetIndex("Sheet 1"));
Assert.AreEqual(2, wb.GetSheetIndex("Sheet 2"));
Assert.AreEqual(3, wb.GetSheetIndex("Sheet 3"));
Assert.AreEqual(4, wb.GetSheetIndex("Sheet 4"));
Assert.AreEqual(5, wb.GetSheetIndex("Sheet 5"));
Assert.AreEqual(6, wb.GetSheetIndex("Sheet 6"));
Assert.AreEqual(7, wb.GetSheetIndex("Sheet 7"));
Assert.AreEqual(8, wb.GetSheetIndex("Sheet 8"));
Assert.AreEqual(9, wb.GetSheetIndex("Sheet 9"));
// Change
wb.Workbook.SetSheetOrder("Sheet 6", 0);
wb.Workbook.SetSheetOrder("Sheet 3", 7);
wb.Workbook.SetSheetOrder("Sheet 1", 9);
// Check they're currently right
Assert.AreEqual(0, wb.GetSheetIndex("Sheet 6"));
Assert.AreEqual(1, wb.GetSheetIndex("Sheet 0"));
Assert.AreEqual(2, wb.GetSheetIndex("Sheet 2"));
Assert.AreEqual(3, wb.GetSheetIndex("Sheet 4"));
Assert.AreEqual(4, wb.GetSheetIndex("Sheet 5"));
Assert.AreEqual(5, wb.GetSheetIndex("Sheet 7"));
Assert.AreEqual(6, wb.GetSheetIndex("Sheet 3"));
Assert.AreEqual(7, wb.GetSheetIndex("Sheet 8"));
Assert.AreEqual(8, wb.GetSheetIndex("Sheet 9"));
Assert.AreEqual(9, wb.GetSheetIndex("Sheet 1"));
// Read it in and see if it is correct.
MemoryStream baos = new MemoryStream();
wb.Write(baos);
MemoryStream bais = new MemoryStream(baos.ToArray());
HSSFWorkbook wbr = new HSSFWorkbook(bais);
Assert.AreEqual(0, wbr.GetSheetIndex("Sheet 6"));
Assert.AreEqual(1, wbr.GetSheetIndex("Sheet 0"));
Assert.AreEqual(2, wbr.GetSheetIndex("Sheet 2"));
Assert.AreEqual(3, wbr.GetSheetIndex("Sheet 4"));
Assert.AreEqual(4, wbr.GetSheetIndex("Sheet 5"));
Assert.AreEqual(5, wbr.GetSheetIndex("Sheet 7"));
Assert.AreEqual(6, wbr.GetSheetIndex("Sheet 3"));
Assert.AreEqual(7, wbr.GetSheetIndex("Sheet 8"));
Assert.AreEqual(8, wbr.GetSheetIndex("Sheet 9"));
Assert.AreEqual(9, wbr.GetSheetIndex("Sheet 1"));
// Now get the index by the sheet, not the name
for (int i = 0; i < 10; i++)
{
NPOI.SS.UserModel.ISheet s = wbr.GetSheetAt(i);
Assert.AreEqual(i, wbr.GetSheetIndex(s));
}
}
示例5: DownloadResult
public ActionResult DownloadResult(string ontologyCode, string fileName)
{
OntologyDescriptor ontology;
if (!AcDomain.NodeHost.Ontologies.TryGetOntology(ontologyCode, out ontology))
{
throw new ValidationException("非法的本体码" + ontologyCode);
}
string dirPath = Server.MapPath("~/Content/Import/Excel/" + ontology.Ontology.Code + "/" + AcSession.Account.Id);
string fullName = Path.Combine(dirPath, fileName);
if (!System.IO.File.Exists(fullName))
{
throw new ValidationException("下载的文件不存在" + fullName);
}
// 操作Excel
FileStream fs = System.IO.File.OpenRead(fullName);
IWorkbook workbook = new HSSFWorkbook(fs);//从流内容创建Workbook对象
fs.Close();
ISheet sheet = workbook.GetSheet(ResultSheetName);
var sheetIndex = workbook.GetSheetIndex(sheet);
for (int i = 0; i < workbook.NumberOfSheets; i++)
{
if (i != sheetIndex)
{
workbook.RemoveSheetAt(i);
}
}
sheetIndex = workbook.GetSheetIndex("Failed");
if (sheetIndex >= 0)
{
workbook.RemoveSheetAt(sheetIndex);
}
ISheet failedSheet = workbook.CreateSheet("Failed");
if (sheet.LastRowNum == 2)
{
throw new ValidationException("没有待导入数据");
}
int rowIndex = 0;
IRow headRow0 = sheet.GetRow(rowIndex);
var columnWidthDic = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
for (int i = 0; i < headRow0.Cells.Count; i++)
{
var cell = headRow0.Cells[i];
columnWidthDic.Add(cell.SafeToStringTrim(), sheet.GetColumnWidth(i));
}
IRow failedRow0 = failedSheet.CreateRow(rowIndex);
var cells = headRow0.Cells;
for (int i = 0; i < cells.Count; i++)
{
var cell = failedRow0.CreateCell(i);
cell.CellStyle = cells[i].CellStyle;
cell.SetCellValue(cells[i].SafeToStringTrim());
}
rowIndex++;
IRow headRow1 = sheet.GetRow(rowIndex);
IRow failedRow1 = failedSheet.CreateRow(rowIndex);
cells = headRow1.Cells;
for (int i = 0; i < cells.Count; i++)
{
var cell = failedRow1.CreateCell(i);
cell.CellStyle = cells[i].CellStyle;
cell.SetCellValue(cells[i].SafeToStringTrim());
}
rowIndex++;
IRow headRow2 = sheet.GetRow(rowIndex);
IRow failedRow2 = failedSheet.CreateRow(rowIndex);
cells = headRow2.Cells;
for (int i = 0; i < cells.Count; i++)
{
var cell = failedRow2.CreateCell(i);
cell.CellStyle = cells[i].CellStyle;
cell.SetCellValue(cells[i].SafeToStringTrim());
}
failedSheet.CreateFreezePane(0, 3, 0, 3);
rowIndex++;
int resultFailedRowIndex = rowIndex;
int stateCodeIndex = -1;
int localEntityIdIndex = -1;
int infoValueKeysIndex = -1;
int infoIdKeysIndex = -1;
for (int i = 0; i < headRow0.Cells.Count; i++)
{
var value = headRow0.GetCell(i).SafeToStringTrim();
if (CommandColHeader.StateCode.Equals(value, StringComparison.OrdinalIgnoreCase))
{
stateCodeIndex = i;
break;
}
}
if (stateCodeIndex < 0)
{
throw new ValidationException("目标Excel中没有头为$StateCode的列");
}
for (int i = 0; i < headRow0.Cells.Count; i++)
{
var value = headRow0.GetCell(i).SafeToStringTrim();
if (CommandColHeader.LocalEntityId.Equals(value, StringComparison.OrdinalIgnoreCase))
{
localEntityIdIndex = i;
break;
//.........这里部分代码省略.........
示例6: Import
public ActionResult Import(string ontologyCode)
{
if (string.IsNullOrEmpty(ontologyCode))
{
throw new ValidationException("未传入本体码");
}
OntologyDescriptor ontology;
if (!AcDomain.NodeHost.Ontologies.TryGetOntology(ontologyCode, out ontology))
{
throw new ValidationException("非法的本体码" + ontologyCode);
}
string message = "";
if (Request.Files.Count == 0)
{
throw new ValidationException("错误:请上传文件!");
}
HttpPostedFileBase file = Request.Files[0];
if (file == null)
{
throw new ValidationException("错误:请上传文件!");
}
string fileName = file.FileName;
if (string.IsNullOrEmpty(fileName) || file.ContentLength == 0)
{
message = "错误:请上传文件!";
}
else
{
bool isSave = true;
string fileType = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
fileName = fileName.Substring(0, fileName.Length - fileType.Length);
if (file.ContentLength > 1024 * 1024 * 10)
{
message = "错误:文件大小不能超过10M!";
isSave = false;
}
else if (fileType != ".xls")
{
message = "错误:文件上传格式不正确,请上传.xls格式文件!";
isSave = false;
}
if (isSave)
{
string dirPath = Server.MapPath("~/Content/Import/Excel/" + ontology.Ontology.Code + "/" + AcSession.Account.Id);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
string fullName = Path.Combine(dirPath, fileName + Guid.NewGuid().ToString() + fileType);
file.SaveAs(fullName);
int successSum = 0;
int failSum = 0;
try
{
FileStream fs = System.IO.File.OpenRead(fullName);
IWorkbook workbook = new HSSFWorkbook(fs);//从流内容创建Workbook对象
fs.Close();
ICellStyle failStyle = workbook.CreateCellStyle();
ICellStyle successStyle = workbook.CreateCellStyle();
failStyle.BorderBottom = BorderStyle.Thin;
failStyle.BorderLeft = BorderStyle.Thin;
failStyle.BorderRight = BorderStyle.Thin;
failStyle.BorderTop = BorderStyle.Thin;
failStyle.FillForegroundColor = HSSFColor.LightOrange.Index;
failStyle.FillPattern = FillPattern.SolidForeground;
successStyle.BorderBottom = BorderStyle.Thin;
successStyle.BorderLeft = BorderStyle.Thin;
successStyle.BorderRight = BorderStyle.Thin;
successStyle.BorderTop = BorderStyle.Thin;
successStyle.FillForegroundColor = HSSFColor.LightGreen.Index;
successStyle.FillPattern = FillPattern.SolidForeground;
ISheet sheet = null;
// 工作表sheet的命名规则是:本体码 或 本体名 或 ‘工作表’
var sheetNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
ontology.Ontology.Code, ontology.Ontology.Name, "工作表","Failed","失败的","Sheet1"
};
foreach (var sheetName in sheetNames)
{
if (sheet != null)
{
break;
}
int dataSheetIndex = workbook.GetSheetIndex(sheetName);
if (dataSheetIndex >= 0)
{
sheet = workbook.GetSheetAt(dataSheetIndex);
}
}
if (sheet == null)
{
System.IO.File.Delete(fullName);
throw new ValidationException("没有名称为'" + ontology.Ontology.Code + "'或'" + ontology.Ontology.Name + "'或'工作表'的sheet");
}
int sheetIndex = workbook.GetSheetIndex(sheet);
workbook.SetSheetName(sheetIndex, ResultSheetName);
for (int i = 0; i < workbook.NumberOfSheets; i++)
{
if (i != sheetIndex)
//.........这里部分代码省略.........