i had a excel sheet which is coming to a jsp from the servlet. Can someone help me of how to write a jsp page to display the excel contents in the browser.
Thanks in advance.
If the excel is not changing the structure dynamically, I recommend you to build a set of objects and parse these information in the server, and then in the jsp read these objects to build an html table with this set of objects.
You can load an Excel file with the class HSSFWorkbook or XSSFWorkbook, and then loop to the rows and cells for building your objects.
Please have a look at the examples of Apache Poi here
You can do something like this...
if(sheet.getPhysicalNumberOfRows() > 0) {
lastRowNum = sheet.getLastRowNum();
for(int j = 0; j <= lastRowNum; j++) {
row = sheet.getRow(j);
lastCellNum = row.getLastCellNum();
for(int i = 0; i <= lastCellNum; i++) {
cell = row.getCell(i);
if(cell == null) {
csvLine.add("");
}
else { //set your properties here...
}
}
}
}
Related
I'm using npoi to save data in excel sheet,but i have a huge data it takes more than 1 hour when trying to insert data like that so i'm trying to insert data bulk into excel sheet.
public byte[] getWorkbook(DataTable dt)
{
IRow currentRow;
HSSFWorkbook workbook;
var worksheet = workbook.CreateSheet("Sheet1");
for(int i = 0; i < dt.Rows.Count; i++)
{
currentRow=worksheet.CreateRow(i);
for(int j = 0; j < dt.Columns.Count; j++)
{
currentRow.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
worksheet.AutoSizeColumn(j);
}
}
var stream =new MemoryStream();
workbook.Write(stream);
return stream.ToArray();
}
Reading each value from DataTable and moving it cell by cell in excel is a usual way of transferring data. But for bulk data, this procedure is not recommended because of time complexity.
So to overcome this we can use clipboard to copy data from DataTable and we can paste it into the range of excel.It improves performance.
To achieve this, DataTable don't have default function to copy data to clipboard. So we have to convert Datatable to array and from array to sting. And string can be easily copied to clipboard and can be pasted in Excel.
Please find a link for reference here
i use org.apache.poi.poi version 3.17
i want to fine the inclined line in office word2003(doc) tables
for example
enter image description here
i used the XWPFTableRow function
cell.get(index).getCttc().getTcPr().getTcBorders();
to find where is the line in word2007 version (docx)
my code like this :
Rang range = hwpf.getRange();
TableIterator iterator = new TableIterator(range);
while(iterator.hasNext())
{
Table next = iterator.next();
for(int i = 0; i<next.numRows();i++)
{
TableRow row = next.getRow(i);
for(int j =0 ;j<row.numCells();j++)
{
TableCell cell = row.getCell(j);
//now i cant find likeness function to find the line in doc
}
}
}```
but i can`t find likeness function in HWPFDocument
so i need your help
thank you so much
I'm running into an issue with EPPlus when there are more than 65,530 rows that have a column with a hyperlink. The example below is configured to create 65,530 rows. With this number it will create the Excel file correctly (not corrupt). Once you run it with anything over 65,530, the Excel file will be created but when you open it, Excel will report that is corrupt. Any ideas how to solve this issue?
try
{
int maxRowsToCreate = 65530; //-- no errors will be generated
//int maxRowsToCreate = 65531; //-- error will be generated. The Excel file will be created but will give an error when trying to open it.
string report = string.Format("D:\\temp\\hypelinkIssue-{0}.xlsx", maxRowsToCreate.ToString());
if (File.Exists(report))
{
File.Delete(report);
}
using (ExcelPackage pck = new ExcelPackage(new System.IO.FileInfo(report)))
{
//Add the Content sheet
var ws = pck.Workbook.Worksheets.Add("Catalog");
ws.View.ShowGridLines = true;
var namedStyle = pck.Workbook.Styles.CreateNamedStyle("HyperLink"); //This one is language dependent
namedStyle.Style.Font.UnderLine = true;
namedStyle.Style.Font.Color.SetColor(Color.Blue);
ws.Column(1).Width = 100;
int rowIndex = 0;
for (int i = 0; i < maxRowsToCreate; i++)
{
rowIndex += 1;
string fullFilePath = string.Format("D:\\temp\\{0}", Path.GetRandomFileName());
ws.Cells[rowIndex, 1].StyleName = "HyperLink";
ws.Cells[rowIndex, 1].Hyperlink = new Uri(string.Format(#"file:///{0}", fullFilePath));
ws.Cells[rowIndex, 1].Value = fullFilePath;
}
pck.Save();
}
System.Diagnostics.Process.Start(report);
}
catch (Exception ex)
{
throw ex;
}
The issue occurs when using ".Hyperlink". If instead I use ".Formula" and populate it with the "=HYPERLINK" Excel formula, it works fine. I was able to create 250k records with unique hyperlink using this approach. I did not try more than 250k but hopefully it will work fine.
Thanks for pointing me in the right direction.
/*
This only works with LESS than 65,530 hyperlinks
*/
ws.Cells[rowIndex, 1].StyleName = "HyperLink";
ws.Cells[rowIndex, 1].Hyperlink = new OfficeOpenXml.ExcelHyperLink(fullFilePath, ExcelHyperLink.UriSchemeFile);
ws.Cells[rowIndex, 1].Value = fullFilePath;
/*
This works with more that 65,530 hyperlinks
*/
string cellFormula = string.Format("=HYPERLINK(\"{0}\")", filePath);
ws.Cells[rowIndex, 1].Formula = cellFormula;
This is because Excel limits the amount of unique URLs in a file to 65,530. You should try to insert them as text, instead of a url.
For a possible solution, take a look at this answer.
I'm using NPOI Excel Library to generate a Excel file, in that Excel file i'm explicitly define column type for Columns like Date,String etc.
Im using the following code to achive this.
var row = sheet.CreateRow(currentNPOIRowIndex++);
for (var colIndex = 0; colIndex < exportData.Columns.Count; colIndex++)
{
ICell cell = null;
cell = row.CreateCell(colIndex);
if (exportData.Columns[colIndex].DataType == typeof(DateTime))
{
if (exportData.Rows[rowIndex][colIndex].ToString() != "")
{
cell.SetCellValue((DateTime)exportData.Rows[rowIndex][colIndex]);
cell.CellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)book.CreateCellStyle();
cell.CellStyle.DataFormat = book.CreateDataFormat().GetFormat("yyyyMMdd HH:mm:ss");
cell = null;
}
else
cell.SetCellValue(exportData.Rows[rowIndex][colIndex].ToString());
}
else
cell.SetCellValue(exportData.Rows[rowIndex][colIndex].ToString());
}
}
The above code works fine for 42 rows i.e. it correctly set the Column Type,but after 42 rows Column Type doesn't apply.
Any help will be highly appreciated.
you'll required to set default column style if you want to set column format for all cells of that column. Please see the below example from xssf format. Syntax may differ for your hssf format but it will give you idea what you are missing.
I am providing you from my working code. I am using NPOI version 2.2.1.0.
can you comment line //cell = null;
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("Template");
XSSFFont defaultFont = (XSSFFont)workbook.CreateFont();
defaultFont.FontHeightInPoints = (short)10;
XSSFCellStyle headerStyle = (XSSFCellStyle)workbook.CreateCellStyle();
headerStyle.WrapText = true;
XSSFCellStyle defaultStyle = (XSSFCellStyle)workbook.CreateCellStyle();
XSSFDataFormat defaultDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
defaultStyle.SetDataFormat(defaultDataFormat.GetFormat("000-000-0000"));
defaultStyle.FillBackgroundColor = IndexedColors.LightYellow.Index;
defaultStyle.FillForegroundColor = IndexedColors.LightTurquoise.Index;
defaultStyle.SetFont(defaultFont);
var row = sheet.CreateRow(0);
for (int headerCount = 0; headerCount < headers.Count(); headerCount++)
{
row.CreateCell(headerCount).SetCellValue(headers[headerCount]);
row.Cells[headerCount].CellStyle = headerStyle;
sheet.SetDefaultColumnStyle(headerCount, defaultStyle);
}
I currently have an excel file that has multiple worksheets and it takes very long to load. I only need one worksheet from it called "comparisons". May I ask how/if there's a way to open a specific worksheet and not load anything else. Hoping this will improve load time.
If your excel workbook have many sheets, you can compare worksheet name is "comparisons" then you open it. Otherwise don't open. You can use EPPLUS excel library to read excel sheet.
using (var pck = new ExcelPackage())
{
var sheetCount = pck.Workbook.Worksheets.Count;
if (sheetCount > 0)
{
for (var y = 1; y <= sheetCount; y++)
{
if(pck.Workbook.Worksheets[y].Name=="comparisons")
{
// You open sheet.
}
else
{
//Don't open sheet.
}
}
}
}