本文共 3161 字,大约阅读时间需要 10 分钟。
NPOI? Necessary POI ????????????? Excel ??? Java ??????? Java ???? Excel ?????????????? NPOI ?? Excel ?????????????? Excel ?????? Excel ????????????
??? NPOI ?? Excel ?????????? Excel ????????
Excel 2007 (.xlsx)??? XSSFWorkbook ? XSSFSheet ??
XSSFWorkbook wb; XSSFSheet sh;
Excel 2003 (.xls)??? HSSFWorkbook ? HSSFSheet ??
HSSFWorkbook wk = new HSSFWorkbook();ISheet sheet = wk.CreateSheet("Images");??????? Excel ????????
HSSFWorkbook wb; FileStream file;file = new FileStream(filepath, FileMode.Open, FileAccess.Read);wb = new HSSFWorkbook(file);file.Close();// ?????ISheet image_sheet = wk.GetSheet("Images");ISheet confidence_sheet = wk.GetSheet("Confidence");// ?? Excel ????????int rowsCount = image_sheet .PhysicalNumberOfRows;int colsCount = image_sheet .GetRow(0).PhysicalNumberOfCells; ???????? Excel ????????
var workBook = new HSSFWorkbook();ISheet sheet = null;sheet = workBook.CreateSheet("sheet1");// ???????IRow row = sheet.CreateRow(0);row.CreateCell(0).SetCellValue("??");row.CreateCell(1).SetCellValue("??");// ??????workBook.SetActiveSheet(0);// ????string filePath=@"test.xls";using (FileStream fs = new FileStream(filePath, FileMode.Create)){ workBook.Write(fs);} ????????????????????
ICellStyle cellstyle = workbook.CreateCellStyle();cellstyle.VerticalAlignment = VerticalAlignment.Center;cellstyle.Alignment = HorizontalAlignment.Center;cell.CellStyle = cellstyle;// ????????????????for (int i = 0; i <= columnsCount; i++) { sheet.AutoSizeColumn(i);}// ?????????????for (int i = 0; i < 15; i++) { int columnWidth = sheet.GetColumnWidth(i) / 256; for (int rowNum = 4; rowNum < 6 + rowsCount; rowNum++) { IRow currentRow; if (sheet.GetRow(rowNum) == null) { currentRow = sheet.CreateRow(rowNum); } else { currentRow = sheet.GetRow(rowNum); } if (currentRow.GetCell(i) != null) { ICell currentCell = currentRow.GetCell(i); int length = Encoding.Default.GetBytes(currentCell.ToString()).Length; if (columnWidth < length) { columnWidth = length; } } sheet.SetColumnWidth(i, columnWidth * 350); }} ?????? Java ?????? Excel ??????
using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp1 { class Program { static void Main(string[] args) { using (FileStream fs = File.OpenRead(@"F:\PycharmWorkPlace\test2excel\test.xlsx")) { XSSFWorkbook wb = new XSSFWorkbook(fs); ISheet sh = wb.GetSheet("Sheet1"); Console.WriteLine(sh.GetRow(0).GetCell(0).StringCellValue); Console.ReadLine(); } } }} XSSFWorkbook ? HSSFWorkbook??DateTime ? Boolean??????????????????????????????? NPOI ??????? Excel ????????????
转载地址:http://nejfk.baihongyu.com/