Tuesday, 29 August 2017 06:59
Insert image in word document
using NPOI.XWPF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NPOI { class Program { static void Main(string[] args) { //Create document XWPFDocument doc = new XWPFDocument(); XWPFParagraph para = doc.CreateParagraph(); XWPFRun run = para.CreateRun(); //Insert image and set its size using (FileStream picFile = new FileStream("../../../Data/image.jpg", FileMode.Open,FileAccess.Read)) { run.AddPicture(picFile, (int)PictureType.PNG, "image", 300 * 12857, 168 * 12857); } //Save the file using (FileStream file = File.Create("Image.docx")) { doc.Write(file); } //Launch System.Diagnostics.Process.Start("Image.docx"); } } }
Published in
NPOI
Tuesday, 29 August 2017 06:58
Create table in word document
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NPOI.XWPF.UserModel; using System.IO; namespace NPOI { class Program { static void Main(string[] args) { //Create document XWPFDocument doc = new XWPFDocument(); //Create table int row = 3; int col = 2; XWPFTable table = doc.CreateTable(row, col); //Set table width table.Width = 4000; //Loop through the table for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { XWPFTableCell cell = table.GetRow(i).GetCell(j); //Set style of the table XWPFParagraph para=cell.AddParagraph(); XWPFRun run=para.CreateRun(); run.SetBold(true); run.FontFamily = "NSimSun"; run.SetText("Row" + i + " " + "Col" + j); table.GetRow(i).SetHeight(800); para.Alignment = ParagraphAlignment.CENTER; cell.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); } } //Save the file FileStream file = new FileStream("Table.docx", FileMode.Create); doc.Write(file); file.Close(); //Launch System.Diagnostics.Process.Start("Table.docx"); } } }
Published in
NPOI
Tuesday, 29 August 2017 06:56
Create multi-level list numbering in word document
using NPOI.OpenXmlFormats.Wordprocessing; using NPOI.XWPF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NPOI { class Program { static void Main(string[] args) { //Create document XWPFDocument doc = new XWPFDocument(); //Create numbering XWPFNumbering numbering = doc.CreateNumbering(); string abstractNumId = numbering.AddAbstractNum(); string numId = numbering.AddNum(abstractNumId); //Create paragragh and set its list level XWPFParagraph para1 = doc.CreateParagraph(); XWPFRun run1 = para1.CreateRun(); run1.SetText("The first paragraph"); para1.SetNumID(numId, "0"); //Create paragragh and set the list level XWPFParagraph para2 = doc.CreateParagraph(); XWPFRun run2 = para2.CreateRun(); run2.SetText("The second paragraph"); para2.SetNumID(numId, "0"); //Create paragragh and apply multi level list XWPFParagraph para3 = doc.CreateParagraph(); XWPFRun run3 = para3.CreateRun(); run3.SetText("The third paragraph"); para3.SetNumID(numId, "0"); para3 = doc.CreateParagraph(); run3 = para3.CreateRun(); run3.SetText("The first sub-item"); para3.SetNumID(numId, "1"); para3 = doc.CreateParagraph(); run3 = para3.CreateRun(); run3.SetText("The second sub-item"); para3.SetNumID(numId, "1"); para3 = doc.CreateParagraph(); run3 = para3.CreateRun(); run3.SetText("The sub-sub-item"); para3.SetNumID(numId, "2"); //Save the file and Launch using (FileStream sw = new FileStream("ListNumbering.doc", FileMode.Create)) { doc.Write(sw); } System.Diagnostics.Process.Start("ListNumbering.doc"); } } }
Published in
NPOI
Tuesday, 29 August 2017 06:53
Create bookmark in word document
using NPOI.OpenXmlFormats.Wordprocessing; using NPOI.XWPF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NOPI { class Program { static void Main(string[] args) { //Creat document XWPFDocument doc = new XWPFDocument(); CT_P para = doc.Document.body.AddNewP(); //Bookmark start CT_Bookmark bookmark = new CT_Bookmark(); bookmark.name = "MyBookmak"; para.Items.Add(bookmark); int idCount = para.Items.Count; bookmark.id = idCount.ToString(); para.ItemsElementName.Add(ParagraphItemsChoiceType.bookmarkStart); para.AddNewR().AddNewT().Value = "This is the bookmark"; //Bookmark end bookmark = new CT_Bookmark(); bookmark.id = idCount.ToString(); para.Items.Add(bookmark); para.ItemsElementName.Add(ParagraphItemsChoiceType.bookmarkEnd); //Save the file using (FileStream file = File.Create("Bookmark.docx")) { doc.Write(file); } //Launch System.Diagnostics.Process.Start("Bookmark.docx"); } } }
Published in
NPOI
Tuesday, 29 August 2017 06:51
Convert document to other formats in word document
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NPOI.XWPF.UserModel; using System.IO; namespace NPOI { class Program { static void Main(string[] args) { //Load document XWPFDocument doc = new XWPFDocument(new FileStream("../../../Data/Sample.doc", FileMode.Open)); //Save the file using (FileStream sw = File.Create("ConvertedFile.docx")) { doc.Write(sw); } } } }
Published in
NPOI
Tuesday, 29 August 2017 06:41
Add header and footer in word document
using NPOI.OpenXmlFormats.Wordprocessing; using NPOI.XWPF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NPOI { class Program { static void Main(string[] args) { //Create document XWPFDocument doc = new XWPFDocument(); XWPFParagraph para=doc.CreateParagraph(); XWPFRun run = para.CreateRun(); run.SetText("Spire.Doc for .NET"); doc.Document.body.sectPr = new CT_SectPr(); CT_SectPr secPr = doc.Document.body.sectPr; //Create header and set its text CT_Hdr header = new CT_Hdr(); header.AddNewP().AddNewR().AddNewT().Value = "This is Header"; //Create footer and set its text CT_Ftr footer = new CT_Ftr(); footer.AddNewP().AddNewR().AddNewT().Value = "This is Footer"; //Create the relation of header XWPFRelation relation1 = XWPFRelation.HEADER; XWPFHeader myHeader = (XWPFHeader)doc.CreateRelationship(relation1, XWPFFactory.GetInstance(), doc.HeaderList.Count + 1); //Create the relation of footer XWPFRelation relation2 = XWPFRelation.FOOTER; XWPFFooter myFooter = (XWPFFooter)doc.CreateRelationship(relation2, XWPFFactory.GetInstance(), doc.FooterList.Count + 1); //Set the header myHeader.SetHeaderFooter(header); CT_HdrFtrRef myHeaderRef = secPr.AddNewHeaderReference(); myHeaderRef.type = ST_HdrFtr.@default; myHeaderRef.id = myHeader.GetPackageRelationship().Id; //Set the footer myFooter.SetHeaderFooter(footer); CT_HdrFtrRef myFooterRef = secPr.AddNewFooterReference(); myFooterRef.type = ST_HdrFtr.@default; myFooterRef.id = myFooter.GetPackageRelationship().Id; //Save the file using (FileStream stream = File.Create("HeaderAndFooter.docx")) { doc.Write(stream); } //Launch System.Diagnostics.Process.Start("HeaderAndFooter.docx"); } } }
Published in
NPOI
Monday, 28 August 2017 07:01
Set cell style in Excel
using NPOI.HSSF.Util; 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 NPOI { class Program { static void Main(string[] args) { //Create workbook IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("MySheet"); //Create cell and set its value ICell cell = sheet.CreateRow(1).CreateCell(3); cell.SetCellValue("Spire"); //Create style ICellStyle style = workbook.CreateCellStyle(); //Set border style style.BorderBottom = BorderStyle.Double; style.BottomBorderColor = HSSFColor.Yellow.Index; //Set font style IFont font = workbook.CreateFont(); font.Color = HSSFColor.Red.Index; font.FontName = "Arial"; font.FontHeight = 13; font.IsItalic = true; style.SetFont(font); //Set background color style.FillBackgroundColor = IndexedColors.Black.Index; style.FillPattern = FillPattern.SolidForeground; //Apply the style cell.CellStyle = style; //Save the file FileStream file = File.Create("CellStyle.xlsx"); workbook.Write(file); file.Close(); //Launch the file System.Diagnostics.Process.Start("CellStyle.xlsx"); } } }
Published in
NPOI
Monday, 28 August 2017 06:59
Protect excel worksheet
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 NPOI { class Program { static void Main(string[] args) { //Create workbook IWorkbook workbook = new XSSFWorkbook(); //Create a new sheet ISheet sheet = workbook.CreateSheet("newSheet"); //Protect the sheet sheet.ProtectSheet("test"); //Save the file FileStream file = File.Create("Protected.xlsx"); workbook.Write(file); file.Close(); //Launch System.Diagnostics.Process.Start("Protected.xlsx"); } } }
Published in
NPOI
Monday, 28 August 2017 06:56
Merge cells in Excel
using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NOPI { class Program { static void Main(string[] args) { //Create workbook IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("MySheet"); //Set the value of the cell sheet.CreateRow(0).CreateCell(0).SetCellValue("Spire.XLS"); //Merge the cell CellRangeAddress region = new CellRangeAddress(0, 5, 0, 5); sheet.AddMergedRegion(region); //Save the file FileStream file = File.Create("ExcelMerge.xlsx"); workbook.Write(file); file.Close(); //Launch the file System.Diagnostics.Process.Start("ExcelMerge.xlsx"); } } }
Published in
NPOI
Monday, 28 August 2017 06:45
Insert image in Excel
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 NPOI { class Program { static void Main(string[] args) { //Load workbook IWorkbook workbook = new XSSFWorkbook(new FileStream("../../../Data/Sample.xlsx", FileMode.Open)); //Get the first sheet ISheet sheet = workbook.GetSheetAt(0); //Add picture data to the workbook byte[] bytes = File.ReadAllBytes("../../../Data/image.jpg"); workbook.AddPicture(bytes, PictureType.JPEG); //Add a picture shape and set its position IDrawing drawing = sheet.CreateDrawingPatriarch(); IClientAnchor anchor = workbook.GetCreationHelper().CreateClientAnchor(); anchor.Dx1 = 0; anchor.Dy1 = 0; anchor.Col1 = 9; anchor.Row1 = 10; IPicture picture = drawing.CreatePicture(anchor, 0); //Automatically adjust the image size picture.Resize(); //Save the file FileStream file = File.Create("ExcelImage.xlsx"); workbook.Write(file); file.Close(); //Launch System.Diagnostics.Process.Start("ExcelImage.xlsx"); } } }
Published in
NPOI