用于生成 Office 文件的 .NET 组件。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
7.7 KiB

7 years ago
using System;
8 years ago
using System.IO;
using System.Collections.Generic;
7 years ago
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.Util;
using NPOI.XWPF.UserModel;
namespace Npoi.Samples.CreateNewSpreadsheet
{
public class Program
{
public static void Main(string[] args) {
6 years ago
ExportExcel();
8 years ago
//ExportExcelHSSF();
6 years ago
ExportWord();
7 years ago
//ImportExcelHSSF();
8 years ago
}
private static void ImportExcelHSSF() {
Stream templateStream = new MemoryStream();
7 years ago
using (var file = new FileStream("D:\\2007.xlsx", FileMode.Open, FileAccess.Read)) {
8 years ago
file.CopyTo(templateStream);
templateStream.Seek(0, SeekOrigin.Begin);
}
7 years ago
var templateWorkbook = new XSSFWorkbook(templateStream);
//var sheet = templateWorkbook.GetSheetAt(0);
ISheet sheet = templateWorkbook.CreateSheet("Test") as XSSFSheet;
sheet.SetAutoFilter(new CellRangeAddress(0,sheet.LastRowNum,0,2));
var fileStream = new FileStream("D:\\moban2.xls", FileMode.Create);
templateWorkbook.Write(fileStream);
fileStream.Close();
//Console.WriteLine(templateWorkbook.GetSheetAt(0).PhysicalNumberOfRows);
}
8 years ago
class XXX
{
public int Cell { get; set; }
public string Value { get; set; }
}
private static void ImportExcel() {
var newFile = "newbook2.core.xlsx";
var celldata = new List<XXX>{
new XXX{ Cell =0,Value="00000"},
new XXX{ Cell = 1,Value = "1111111" }
};
using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) {
//excelPath-模板路径,为传递过来的参数
IWorkbook wb = new XSSFWorkbook("newbook.core.xlsx");
ISheet sheet1 = wb.GetSheetAt(0);
//celldata-需要插入的数据,参数
foreach (var x in celldata) {
IRow row = sheet1.GetRow(x.Cell);
row.GetCell(x.Cell).SetCellValue(x.Value);
}
wb.Write(fs);
}
}
private static void ExportExcelHSSF() {
var newFile = @"newbook.core.xls";
using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) {
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet1");
sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
//ICreationHelper cH = wb.GetCreationHelper();
var rowIndex = 0;
IRow row = sheet1.CreateRow(rowIndex);
row.Height = 30 * 80;
var cell = row.CreateCell(0);
var font = workbook.CreateFont();
font.IsBold = true;
font.Color = HSSFColor.DarkBlue.Index2;
cell.CellStyle.SetFont(font);
cell.SetCellValue("A very long piece of text that I want to auto-fit innit, yeah. Although if it gets really, really long it'll probably start messing up more.");
sheet1.AutoSizeColumn(0);
rowIndex++;
// 新增試算表。
var sheet2 = workbook.CreateSheet("My Sheet");
// 建立儲存格樣式。
var style1 = workbook.CreateCellStyle();
style1.FillForegroundColor = HSSFColor.Blue.Index2;
style1.FillPattern = FillPattern.SolidForeground;
var style2 = workbook.CreateCellStyle();
style2.FillForegroundColor = HSSFColor.Yellow.Index2;
style2.FillPattern = FillPattern.SolidForeground;
// 設定儲存格樣式與資料。
var cell2 = sheet2.CreateRow(0).CreateCell(0);
cell2.CellStyle = style1;
cell2.SetCellValue(0);
cell2 = sheet2.CreateRow(1).CreateCell(0);
cell2.CellStyle = style2;
cell2.SetCellValue(1);
cell2 = sheet2.CreateRow(2).CreateCell(0);
cell2.CellStyle = style1;
cell2.SetCellValue(2);
cell2 = sheet2.CreateRow(3).CreateCell(0);
cell2.CellStyle = style2;
cell2.SetCellValue(3);
cell2 = sheet2.CreateRow(4).CreateCell(0);
cell2.CellStyle = style1;
cell2.SetCellValue(4);
workbook.Write(fs);
}
Console.WriteLine("Excel Done");
}
private static void ExportExcel() {
8 years ago
var newFile = @"newbook.core.xlsx";
using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) {
8 years ago
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet1");
sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
//ICreationHelper cH = wb.GetCreationHelper();
var rowIndex = 0;
IRow row = sheet1.CreateRow(rowIndex);
row.Height = 30 * 80;
var cell = row.CreateCell(0);
var font = workbook.CreateFont();
font.IsBold = true;
font.Color = HSSFColor.DarkBlue.Index2;
cell.CellStyle.SetFont(font);
8 years ago
cell.SetCellValue("A very long piece of text that I want to auto-fit innit, yeah. Although if it gets really, really long it'll probably start messing up more.");
8 years ago
sheet1.AutoSizeColumn(0);
rowIndex++;
// 新增試算表。
var sheet2 = workbook.CreateSheet("My Sheet");
// 建立儲存格樣式。
var style1 = workbook.CreateCellStyle();
style1.FillForegroundColor = HSSFColor.Blue.Index2;
style1.FillPattern = FillPattern.SolidForeground;
var style2 = workbook.CreateCellStyle();
style2.FillForegroundColor = HSSFColor.Yellow.Index2;
style2.FillPattern = FillPattern.SolidForeground;
// 設定儲存格樣式與資料。
var cell2 = sheet2.CreateRow(0).CreateCell(0);
cell2.CellStyle = style1;
cell2.SetCellValue(0);
cell2 = sheet2.CreateRow(1).CreateCell(0);
cell2.CellStyle = style2;
cell2.SetCellValue(1);
cell2 = sheet2.CreateRow(2).CreateCell(0);
cell2.CellStyle = style1;
cell2.SetCellValue(2);
cell2 = sheet2.CreateRow(3).CreateCell(0);
cell2.CellStyle = style2;
cell2.SetCellValue(3);
cell2 = sheet2.CreateRow(4).CreateCell(0);
cell2.CellStyle = style1;
cell2.SetCellValue(4);
workbook.Write(fs);
}
8 years ago
Console.WriteLine("Excel Done");
}
8 years ago
private static void ExportWord() {
var newFile2 = @"newbook.core.docx";
using (var fs = new FileStream(newFile2, FileMode.Create, FileAccess.Write)) {
XWPFDocument doc = new XWPFDocument();
var p0 = doc.CreateParagraph();
p0.Alignment = ParagraphAlignment.LEFT;
XWPFRun r0 = p0.CreateRun();
r0.FontFamily = "宋体";
r0.FontSize = 18;
r0.IsBold = true;
r0.SetText("未登录过学生的账号密码");
doc.Write(fs);
}
Console.WriteLine("Word Done");
}
}
8 years ago
}