How to disable gridlines in Excel using open xml C#? - excel

I want to disable GridLines in excel and put custom borders to excel cells using open xml in C#
I have tried with below code but is throwing exception when i open the excell,
the exception is "Repaired Part: /xl/worksheets/sheet.xml part with XML error. Load error. Line 1, column 0."
using (SpreadsheetDocument xl = SpreadsheetDocument.Create(sFile, SpreadsheetDocumentType.Workbook))
{
WorkbookPart wbp = xl.AddWorkbookPart();
WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();
Workbook wb = new Workbook();
FileVersion fv = new FileVersion();
fv.ApplicationName = "Microsoft Office Excel";
Worksheet ws = new Worksheet();
SheetViews sheetViews = new SheetViews();
SheetView sheetView = new SheetView();
sheetView.ShowGridLines = new BooleanValue(false);
sheetViews.Append(sheetView);
ws.Append(sheetViews);
WorkbookStylesPart wbsp = wbp.AddNewPart<WorkbookStylesPart>();
//// add styles to sheet
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
//// add styles to sheet
////wbsp.Stylesheet = GenerateStyleSheet();
//wbsp.Stylesheet.Save();
Columns columns = new Columns();
columns.Append(CreateColumnData(1, 1, 25));
ws.Append(columns);
//// generate rows
SheetData sd = CreateSheetData(products);
ws.Append(sd);
wsp.Worksheet = ws;
wsp.Worksheet.Save();
MERGEiNITIALcELLS(wsp);
wb.Append(fv);
CreateSheet(wbp, wsp, wb);
xl.WorkbookPart.Workbook = wb;
xl.WorkbookPart.Workbook.Save();
xl.Close();

The WorkbookViewId property of the SheetView class is a required attribute/property. Try this:
SheetView sheetView = new SheetView();
sheetView.ShowGridLines = new BooleanValue(false);
sheetView.WorkbookViewId = 0;
sheetViews.Append(sheetView);
ws.Append(sheetViews);
That uses the 1st (default) workbook view. Don't worry, you don't have to explicitly create a WorkbookView child of the BookViews class, which is a child of Workbook. Unless you want to, of course. :)

I tried this way but it was failing but able to identify what is missing. By default when creating a spreadsheet from scratch, there is no default workbook view. So need to create new workbook view.
spreadSheet.WorkbookPart.Workbook = new Workbook(new BookViews(new WorkbookView()));
Then create sheet view.
worksheetPart.Worksheet = new Worksheet(new SheetViews(new SheetView(){WorkbookViewId=0,ShowGridLines=new BooleanValue(false)}), new SheetData());
NOTE: When create columns in the excel workbook, the elements should be created in a sequence. The sequence is
Sheet Views
Sheet View Formatting
Columns
Sheet Data
Enjoy Open XML SDK but Microsoft does not provide very powerfull documentation.

Related

Finding difficulty to add one more sheet in Excel workbook using Apache POI

My following 3 line code is used to create one file with sheet1(Year 2019) now would like to execute another page and need to store the result of it in same file as sheet2(Year 2020).
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Year 2019");
FileOutputStream fos = new FileOutputStream("C:\\Users\\dp\\Desktop\\MData1_Test_1.xlsx");
Row header_1 = sheet.createRow(rowNum[0]);
rowNum[0] = (rowNum[0] + 1);
header_1.createCell(0).setCellValue("Analytics");
Any help will be appreciated.
You can do something like this:
// open your first file
try (InputStream inp = new FileInputStream(fut.get())) {
Workbook wb = WorkbookFactory.create(inp);
// get sheet by name
Sheet sheet = wb.getSheet("Year 2019");
if (null != sheet) {
// if sheet exists => delete it
int index = wb.getSheetIndex(String name);
wb.removeSheetAt(index);
}
// create new sheet
sheet = wb.createSheet("Year 2019");
// proceed adding data
// row = sheet.createRow(0);
// row.createCell(0).setCellValue("data");
// ...
}
You can repeat these steps as many time as you need to manage as many sheets as you want.

How to generate an excel file and send it in the response using spring boot

I have a list of employee records in a table, i want to generate report out of it. Is there a way to generate and send the generated report in excel format to the browser in response.FYI I'm using Spring boot and reactjs.
You probably want to look at Apache POI
There are some examples of creating XLS[X] files in their developers guide
A quick example from there
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
// Write the output to a file
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}

Make a WordArt not recieve focus

Is there an API in VSTO/VBA for Excel or Aspose Cells for Java to make it so that a WordArt I added to the Excel worksheet never receives keyboard or mouse focus?
For e.g. in the picture below, when I click on the WordArt, it receives focus. When it has focus and I hit the Tab key on the keyboard, the focus goes to the next WordArt, i.e. the one above it.
I'd like these WordArt objects never to receive keyboard or mouse focus. Is there a way?
This can be achieved by protecting your worksheet. You can learn from internet how to protect worksheet and its contents or objects. Aspose.Cells supports the protection of worksheet.
Please see the following sample code which uses Aspose.Cells APIs. It explains how to protect your worksheet contents or objects. Please modify the code as per your needs.
Both Java and C# codes are given for a reference.
Java
//Load your workbook
Workbook wb = new Workbook("sample.xlsx");
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Unlock all cells of your worksheet
Style st = ws.getCells().get("A1").getStyle();
st.setLocked(false);
StyleFlag flg = new StyleFlag();
flg.setLocked(true);
ws.getCells().applyStyle(st, flg);
//Specify protection types
Protection p = ws.getProtection();
p.setAllowEditingContent(false);
p.setAllowEditingObject(false);
p.setAllowEditingScenario(false);
//Protect the worksheet
ws.protect(ProtectionType.ALL);
//Save the workbook
wb.save("output.xlsx");
C#
//Load your workbook
Workbook wb = new Workbook("sample.xlsx");
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Unlock all cells of your worksheet
Style st = ws.Cells["A1"].GetStyle();
st.IsLocked = false;
StyleFlag flg = new StyleFlag();
flg.Locked = true;
ws.Cells.ApplyStyle(st, flg);
//Specify protection types
Protection p = ws.Protection;
p.AllowEditingContent = false;
p.AllowEditingObject = false;
p.AllowEditingScenario = false;
//Protect the worksheet
ws.Protect(ProtectionType.All);
//Save the workbook
wb.Save("output.xlsx");
Note: I am working as Developer Evangelist at Aspose
Well, in Aspose.Cells APIs, you may try using certain locking attributes of the wordart shape to accomplish your task, see the sample code for your reference:
e.g
Sample code:
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0);
Shape wordart = worksheet.getShapes().addTextEffect(MsoPresetTextEffect.TEXT_EFFECT_1, "CONFIDENTIAL", "Open Sans", 50, false, true, 18, 8, 1, 1, 130, 800);
//Lock Shape Aspects.
wordart.setLocked(true);
wordart.setLockedProperty(ShapeLockType.SELECTION, true);
wordart.setLockedProperty(ShapeLockType.SHAPE_TYPE, true);
wordart.setLockedProperty(ShapeLockType.MOVE, true);
wordart.setLockedProperty(ShapeLockType.RESIZE, true);
wordart.setLockedProperty(ShapeLockType.TEXT, true);
FillFormat wordArtFormat = wordart.getFill();
wordArtFormat.setFillType(FillType.SOLID);
wordArtFormat.getSolidFill().setColor(Color.getLightGray());
wordArtFormat.setTransparency(0.55);
wordart.setHasLine(false);
workbook.save("out1.xlsx");
Hope, this helps a bit.
I am working as Support developer/Evangelist at Aspose.

How to create excel file with multiple sheets from DataSet using C#

How to create excel file with multiple sheets from DataSet using C#?
I have successfully created an excel file with single sheet. But I am not able to do that for multiple sheets.
Here is a simple C# class that programatically creates an Excel WorkBook and adds two sheets to it, and then populates both sheets. Finally, it saves the WorkBook to a file in the application root directory so that you can inspect the results...
public class Tyburn1
{
object missing = Type.Missing;
public Tyburn1()
{
Excel.Application oXL = new Excel.Application();
oXL.Visible = false;
Excel.Workbook oWB = oXL.Workbooks.Add(missing);
Excel.Worksheet oSheet = oWB.ActiveSheet as Excel.Worksheet;
oSheet.Name = "The first sheet";
oSheet.Cells[1, 1] = "Something";
Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing)
as Excel.Worksheet;
oSheet2.Name = "The second sheet";
oSheet2.Cells[1, 1] = "Something completely different";
string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
+ "\\SoSample.xlsx";
oWB.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook,
missing, missing, missing, missing,
Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
oWB.Close(missing, missing, missing);
oXL.UserControl = true;
oXL.Quit();
}
}
To do this, you would need to add a reference to Microsoft.Office.Interop.Excel to your project (you may have done this already since you are creating one sheet).
The statement that adds the second sheet is...
Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing)
as Excel.Worksheet;
the '1' argument specifies a single sheet, and it can be more if you want to add several sheets at once.
Final note: the statement oXL.Visible = false; tells Excel to start in silent mode.
var groupedSheetList = UserData
.GroupBy (u => u.date)
.Select (grp => grp.ToList ())
.ToList ();
You can try this
using (var package = new ExcelPackage ())
{
foreach (var item in groupedSheetList) {
var workSheet = package.Workbook.Worksheets.Add (item[0].date);
}
}

Make Excel Sheet With Multiple Ultra Win Grid (UltraGridExcelExport)

Dear All Friends
I Have One Window Form Which Have 5 Grid Of Infragistics Ultra Grid. I Want To Export All Grid TO Excel File. Now Problem Is That. In Need All 5 Grids In On Sheet As Shown As Form. So Please Help To Solve.
Currently I Export Only One Grid To Excel Using Infragistics Inbuild Method
UltraGridExport.Export(UltraGrid,FileName)
Suggest Me With Multiple Grid Export In One Sheet.
Thank To All.
How about exporting the results into multiple sheets on the workbook?
Create a workbook and then for each grid create a worksheet on the workbook and then export the grid to the worksheet.
public static void ExportToExcel<T>(this IEnumerable<T> grids, string filename)
where T : UltraGrid
{
Workbook workbook = new Workbook();
UltraGridExcelExporter exporter = new UltraGridExcelExporter();
foreach (T g in grids)
{
Worksheet sheet = workbook.Worksheets.Add(g.Name);
exporter.Export(g.GridControl, sheet);
}
workbook.Save(filename);
}
string StrFile = Application.StartupPath + "\\EmpLevDetView.xls";
Workbook WB = new Workbook();
WB.Worksheets.Add("Detail");
ExcelExport.Export(UltGrdDet, WB.Worksheets["Detail"], 0, 0);
WB.Worksheets.Add("Summary");
ExcelExport.Export(UltGrdSum, WB.Worksheets["Summary"], 0, 0);
BIFF8Writer.WriteWorkbookToFile(WB, StrFile);
System.Diagnostics.Procesenter code heres.Start(StrFile);

Resources