Use DocumentFormat.Openxml 2.8.1.
I try to create Excel Sheet and create Table inside it.
But when i do it - and try to open excel file - excel says that can not open and try to restore this file.
So, i create excel file:
var fStream = new FileStream(tempPathName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 1048, FileOptions.DeleteOnClose);
SpreadsheetDocument ssDoc = SpreadsheetDocument.Create(inputStream,
SpreadsheetDocumentType.Workbook);
Then , create sheet:
var tempPathName = Path.GetTempFileName();
WorkbookPart workbookPart = ssDoc.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
Sheets sheets = ssDoc.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
WorksheetPart worksheetPart4 = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart4.Worksheet = new Worksheet();
Worksheet workSheet4 = worksheetPart4.Worksheet;
var table = CreateTable1();
TableParts tableParts = new TableParts() { Count = (UInt32)1 };
TablePart tablePart = new TablePart() { Id = "rId" + 1 };
tableParts.Append(table);
tableParts.Append(tablePart);
workSheet4.AppendChild(tableParts);
Sheet sheet4 = new Sheet()
{
Id = ssDoc.WorkbookPart.GetIdOfPart(worksheetPart4),
SheetId = 4,
Name = "test"
};
sheets.Append(sheet4);
ssDoc.Close();
private Table CreateTable1()
{
// First, we create the table, its properties and we append it.
Table table = new Table();
TableProperties props = new TableProperties();
table.AppendChild<TableProperties>(props);
// Now we create a new layout and make it "fixed".
TableLayout tl = new TableLayout() { Type = TableLayoutValues.Fixed };
props.TableLayout = tl;
// Then we just create a new row and a few cells and we give them a width
var tr = new TableRow();
var tc1 = new TableCell();
var tc2 = new TableCell();
tc1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2000" }));
tc2.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2000" }));
table.Append(tr);
return table;
}
So, how to create table in Excel sheet and insert data in table?
Thank you.
TableStyle class is what you`re looking for.
You actually have to create TableStyle and add it to your spreadsheet.
In CreateTable1(), TableProperties, TableLayout, TableRow, and TableCell are WordProcessing elements.
Instead, you will want to use code similar to this:
private void CreateTable2(SheetData sheetData)
{
var row1 = new Row(){ RowIndex = 1 };
sheetData.Append(row1);
var cell1 = new Cell();
cell1.DataType = CellValues.InlineString;
cell1.InlineString = new InlineString() { Text = new Text("Hello") };
row1.InsertAt(cell1, 0);
var cell2 = new Cell();
cell2.DataType = CellValues.InlineString;
cell2.InlineString = new InlineString() { Text = new Text("World") };
row1.InsertAt(cell2, 1);
}
Slightly modify the rest of your code to look like the following (most of which I borrowed from here).
var fStream = new FileStream(tempPathName, FileMode.Create);
SpreadsheetDocument ssDoc = SpreadsheetDocument.Create(fStream, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookPart = ssDoc.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = ssDoc.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = ssDoc.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "mySheet"
};
sheets.Append(sheet);
// Get the sheetData cell table.
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
CreateTable2(sheetData);
workbookPart.Workbook.Save();
ssDoc.Close();
fStream.Close();
Related
I was trying to create an excel using Memory Stream. Here is the code I'm trying,
using (MemoryStream mem = new MemoryStream())
{
using (var sheetDocument = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook))
{
//// Add a WorkbookPart to the document.
WorkbookPart workbookpart = sheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
WorkbookStylesPart workbookstyle = workbookpart.AddNewPart<WorkbookStylesPart>();
workbookstyle.Stylesheet = this.GenerateStyleSheet();
workbookstyle.Stylesheet.Save();
//// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
Columns cols = new Columns();
Column col = new Column();
col.BestFit = true;
col.CustomWidth = true;
col.Width = 22.33;
col.Max = 13;
col.Min = 1;
cols.Append(col);
worksheetPart.Worksheet.Append(cols);
SheetData sheetData = new SheetData();
worksheetPart.Worksheet.Append(sheetData);
//// Add Sheets to the Workbook.
Sheets sheets = sheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
//// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = sheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = sheetName };
sheets.Append(sheet);
workbookpart.Workbook.Save();
List<string> columns = new List<string>();
foreach (DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
}
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = this.CreateHeaderRow(columns);
sheetData.AppendChild(headerRow);
foreach (DataRow dataSetRow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = this.CreateDataRow(dataSetRow);
sheetData.AppendChild(newRow);
}
workbookpart.Workbook.Save();
var result = mem.ToArray();
System.IO.File.WriteAllBytes(fileName, result);
}
An excel file is getting created, but when I try to open it, I'm getting an error as bellow
If I press yes, it says
The workbook cannot be opened or repaired by Microsoft Excel because
it is corrupt
How can I fix this?
Thanks to #zaikhan's comment, I was able to fix my issue. When I closed the sheetDocument it worked.
using (MemoryStream mem = new MemoryStream())
{
using (var sheetDocument = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook))
{
//// Add a WorkbookPart to the document.
WorkbookPart workbookpart = sheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
WorkbookStylesPart workbookstyle = workbookpart.AddNewPart<WorkbookStylesPart>();
workbookstyle.Stylesheet = this.GenerateStyleSheet();
workbookstyle.Stylesheet.Save();
//// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
Columns cols = new Columns();
Column col = new Column();
col.BestFit = true;
col.CustomWidth = true;
col.Width = 22.33;
col.Max = 13;
col.Min = 1;
cols.Append(col);
worksheetPart.Worksheet.Append(cols);
SheetData sheetData = new SheetData();
worksheetPart.Worksheet.Append(sheetData);
//// Add Sheets to the Workbook.
Sheets sheets = sheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
//// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = sheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = sheetName };
sheets.Append(sheet);
workbookpart.Workbook.Save();
List<string> columns = new List<string>();
foreach (DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
}
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = this.CreateHeaderRow(columns);
sheetData.AppendChild(headerRow);
foreach (DataRow dataSetRow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = this.CreateDataRow(dataSetRow);
sheetData.AppendChild(newRow);
}
workbookpart.Workbook.Save();
sheetDocument.Close();
}
var result = mem.ToArray();
System.IO.File.WriteAllBytes(fileName, result);
}
I have exported the report to Excel and it works fine, but when I print the file the width of the spreadsheet doesn't fit all column into one page. For this to happen I have to change the Page Layout, and set the scaling to fit 1 on width and 43 tall. How can I get this from code?
using (var workbook = SpreadsheetDocument.Create(Savepath, SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new Workbook();
workbook.WorkbookPart.Workbook.Sheets = new Sheets();
//declare our MergeCells here
MergeCells mergeCells = null;
foreach (DataRow dsrow in table.Rows)
{
int innerColIndex = 0;
rowIndex++;
Row newRow = new Row();
foreach (String col in columns)
{
Stylesheet stylesheet1 = new Stylesheet();
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(dsrow[col].ToString());
cell.CellReference = excelColumnNames[innerColIndex] + rowIndex.ToString();
if (table.TableName == "Work Order Report")
{
string cellNameWorkOrder = dsrow[col].ToString();
if (cellNameWorkOrder == "POSTER: 10% MUST HAVE APPROACH AND CLOSE-UP SHOTS - PHOTO OF EACH CREATIVE" || cellNameWorkOrder == "BULLETINS: 100% CLOSE-UP AND APPROACH OF EACH UNIT")
{
if (mergeCells == null)
mergeCells = new MergeCells();
var cellAddress = cell.CellReference;
var cellAddressTwo = "I" + rowIndex.ToString();
mergeCells.Append(new MergeCell() { Reference = new StringValue(cellAddress + ":" + cellAddressTwo) });
}
}
newRow.AppendChild(cell);
innerColIndex++;
}
sheetData.AppendChild(newRow);
}
//add the mergeCells to the worksheet if we have any
if (mergeCells != null)
sheetPart.Worksheet.InsertAfter(mergeCells, sheetPart.Worksheet.Elements<SheetData>().First());
}
workbook.WorkbookPart.Workbook.Save();
}
The excel report looks like https://www.screencast.com/t/CCMR96Mw7u when I print now its like https://www.screencast.com/t/MkTpDc98RD0l ,https://www.screencast.com/t/MRyzpEiFICM the expected result is https://www.screencast.com/t/ztgvm6mISSwp
In order to achieve this you need to do two things. Firstly, you need to add a PageSetupProperties instance to a SheetProperties instance which in turn should get added to your Worksheet. The PageSetupProperties has a FitToPage property which is the part that sets the radio button in Excel to "Fit to".
Next, you need to use the PageSetup class in order to set the width and height required. This is done via the FitToWidth and FitToHeight properties. The PageSetup also needs to be added to the Worksheet.
Note that the order of the elements is important, you can see the correct order in the ECMA spec.
The following is a self contained example that adds one cell and then sets the properties the way that you need them:
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookpart = myDoc.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
SheetData sheetData = new SheetData();
//add a row
Row row = new Row();
row.RowIndex = 1;
//create a cell
Cell cell = new Cell();
cell.CellReference = "A1";
CellValue cellValue = new CellValue();
cellValue.Text = "123";
cell.Append(cellValue);
row.AppendChild(cell);
sheetData.AppendChild(row);
// Add a WorkbookPart to the document.
worksheetPart.Worksheet = new Worksheet(sheetData);
//this sets the "Fit to" radio in Excel.
//note this must come before the SheetData
SheetProperties sheetProperties = new SheetProperties();
PageSetupProperties pageSetupProperties = new PageSetupProperties() { FitToPage = true };
sheetProperties.Append(pageSetupProperties);
worksheetPart.Worksheet.InsertBefore(sheetProperties, sheetData);
// this changes the fit to width and height
PageSetup pageSetup = new PageSetup() { FitToWidth = 1, FitToHeight = 43 };
worksheetPart.Worksheet.AppendChild(pageSetup);
//append the sheets / sheet
Sheets sheets = myDoc.WorkbookPart.Workbook.AppendChild(new Sheets());
sheets.AppendChild(new Sheet()
{
Id = myDoc.WorkbookPart.GetIdOfPart(myDoc.WorkbookPart.WorksheetParts.First()),
SheetId = 1,
Name = "Sheet1"
});
}
I inserted/appended new sheet in existing Excel file using open XML but I am not able to add rows and cell values. Following is my code:
using (var workbook = SpreadsheetDocument.Open(excelFilePath, true))
{
var workbookPart = workbook.WorkbookPart;
var wb = workbookPart.Workbook;
int rowIndex = 0;
// Add a blank WorksheetPart.
WorksheetPart newWorksheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(newWorksheetPart);
// Get a unique ID for the new worksheet.
uint sheetId = 1;
if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
// Give the new worksheet a name.
string sheetName = "Data";
// Append the new worksheet and associate it with the workbook.
Sheet dataSheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
workbookPart.Workbook.Save();
var sharedStringPart = workbookPart.SharedStringTablePart;
var values = sharedStringPart.SharedStringTable.Elements<SharedStringItem>().ToArray();
Row headerRow = new Row();
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue("EmpId");
headerRow.AppendChild<Cell>(cell);
cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue("Name");
headerRow.AppendChild<Cell>(cell);
headerRow.AppendChild<Cell>(cell);
dataSheet.InsertAt<Row>(headerRow, rowIndex++);
workbookPart.Workbook.Save();
}
}
It's throwing an exception:
Non-composite elements do not have child elements.
When i got this error ("Non-composite elements do not have child elements.") was about my sheet object. I got the worng object to Apeend, so you need change the use .Append to something like that:
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
sheetData.Append(row);
A Full Example
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(tmp, SpreadsheetDocumentType.Workbook))
{
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = 1,
Name = fileName,
};
sheets.Append(sheet);
var firstChild = sheet.FirstChild;
//sheet.Append(row);
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
UInt32 rowIndex = 1;
var row = new Row() { RowIndex = rowIndex };
var firstNameCell = new Cell() { CellReference = "A" + rowIndex };
firstNameCell.CellValue = new CellValue("FirstName");
firstNameCell.DataType = CellValues.String;
row.Append(firstNameCell);
Cell lastNameCell = new Cell() { CellReference = "B" + rowIndex };
lastNameCell.CellValue = new CellValue("LastName");
lastNameCell.DataType = new EnumValue<CellValues>(CellValues.String);
row.Append(lastNameCell);
sheetData.Append(row);
workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();
}
How can we able to write next row of the excel sheet if it ave records during exporting data from c#
it is the code i used for exporting data. to excel . i failed to export the data to next line by this code
private void ExportToOxml(bool firstTime)
{
if (count == 0)
{
ResultsData.Columns.Remove("TotalRecords");
ResultsData.Columns.Remove("PageIndex");
// fileName = #"C:\MyExcel.csv";
//Delete the file if it exists.
if (firstTime && File.Exists(xsfd.FileName))
{
File.Delete(fileName);
}
count++;
}
if (firstTime)
{
//This is the first time of creating the excel file and the first sheet.
// Create a spreadsheet document by supplying the filepath.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
Create(fileName, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new Worksheet(sheetData);
var bold1 = new System.Windows.Documents.Bold();
CellFormat cf = new CellFormat();
// Add Sheets to the Workbook.
Sheets sheets;
sheets = spreadsheetDocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
var sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = sheetId,
Name = "Sheet" + sheetId
};
sheets.Append(sheet);
//Add Header Row.
var headerRow = new Row();
foreach (DataColumn column in ResultsData.Columns)
{
var cell = new Cell { DataType = CellValues.String, CellValue = new CellValue(column.ColumnName) };
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (DataRow row in ResultsData.Rows)
{
var newRow = new Row();
foreach (DataColumn col in ResultsData.Columns)
{
var cell = new Cell
{
DataType = CellValues.String,
CellValue = new CellValue(row[col].ToString())
};
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
workbookpart.Workbook.Save();
spreadsheetDocument.Close();
}
else
{
// Open the Excel file that we created before, and start to add sheets to it.
var spreadsheetDocument = SpreadsheetDocument.Open(fileName, true);
var workbookpart = spreadsheetDocument.WorkbookPart;
if (workbookpart.Workbook == null)
workbookpart.Workbook = new Workbook();
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new Worksheet(sheetData);
var sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
if (sheets.Elements<Sheet>().Any())
{
//Set the new sheet id
sheetId = sheets.Elements<Sheet>().Max(s => s.SheetId.Value) + 1;
}
else
{
sheetId = 1;
}
// Append a new worksheet and associate it with the workbook.
var sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = sheetId,
Name = "Sheet" + sheetId
};
sheets.Append(sheet);
//Add the header row here.
var headerRow = new Row();
foreach (DataColumn column in ResultsData.Columns)
{
var cell = new Cell { DataType = CellValues.String, CellValue = new CellValue(column.ColumnName) };
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (DataRow row in ResultsData.Rows)
{
var newRow = new Row();
foreach (DataColumn col in ResultsData.Columns)
{
var cell = new Cell
{
DataType = CellValues.String,
CellValue = new CellValue(row[col].ToString())
};
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();
}
}
Im using OpenXML SDK 2.5 for creating excel file. I want to hide gridlines on created excel files.I was tried creating SheetViews but it completely hide sheet. My pure code as below
public static SpreadsheetDocument CreateWorkbook(MemoryStream memoryStream)
{
var spreadSheet = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook);
spreadSheet.AddWorkbookPart();
spreadSheet.WorkbookPart.Workbook = new Workbook();
spreadSheet.WorkbookPart.Workbook.Save();
var sharedStringTablePart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>();
sharedStringTablePart.SharedStringTable = new SharedStringTable();
sharedStringTablePart.SharedStringTable.Save();
spreadSheet.WorkbookPart.Workbook.Sheets = new Sheets();
spreadSheet.WorkbookPart.Workbook.Save();
var workbookStylesPart = spreadSheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
workbookStylesPart.Stylesheet = new Stylesheet();
workbookStylesPart.Stylesheet.Save();
memoryStream.Seek(0, SeekOrigin.Begin);
return spreadSheet;
}
public static bool AddWorksheet(SpreadsheetDocument spreadsheet, string name)
{
Sheets sheets = spreadsheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
var worksheetPart = spreadsheet.WorkbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
worksheetPart.Worksheet.Save();
var sheet = new Sheet()
{
Id = spreadsheet.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = (uint)(spreadsheet.WorkbookPart.Workbook.Sheets.Count() + 1),
Name = name
};
if (string.IsNullOrEmpty(name)) {
sheet.Name = string.Format("Sheet{0}", sheet.SheetId);
}
sheets.Append(sheet);
spreadsheet.WorkbookPart.Workbook.Save();
return true;
}
After that im creating instance of spreadsheet in memory
//Creating like that
var memoryStream = new MemoryStream();
var spreadsheet = CreateWorkbook(memoryStream);
AddWorksheet(spreadsheet, null);
//End Filling data
After some check i decide to, when you use the column you couldnt hide gridlines. You should only cell for showing data. But that time you couldnt change width property bacause column doesnt extist :)
Okay. First be sure your pure works properly and generates valid workbook. After that you can apply code:
SheetViews sheetViews = new SheetViews();
SheetView sheetView = new SheetView(){ ShowGridLines = false, TabSelected = true, ZoomScaleNormal = (UInt32Value)100U, WorkbookViewId = (UInt32Value)0U };
Selection selection = new Selection(){ ActiveCell = "A1", SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A1" } };
sheetView.Append(selection);
sheetViews.Append(sheetView);
worksheet.Append(sheetViews);
Be sure that your sheetViews block is placed before sheetdata, or you will have corrupt document.