pdf to excel - placing itext shapes into excel - c#

I am trying to convert a pdf to an excel spreadsheet. I have the code to extract the text with itextsharp ITextChunkLocationStrategy.
I now have a table with one column with text (split by chunks)and another column with chunks rectangle dimensions and locations.
I now need to place the text chunks into the right location on an excel document.
below is code that i have found on googling. but i do not know any way of telling excel which cells to place the text into - based on the coordinates
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
var dty = (from row in dt.AsEnumerable()
orderby row.Field<float>("x")
where row.Field<int>("yRank") == rowNo
select row);
var tbl = dty.CopyToDataTable();
for (int y = 0; y < tbl.Rows.Count; y++)
{
ws.Cells[rowNo, y + 1] = tbl.Rows[y]["text"].ToString();
}
}
rowNo++;

Related

Importing csv file into Excel using EPPlus losing trailing zeroes on data

I'm using C# and EPPlus to import .csv data into a workbook. My only issue is that after the import into Excel, I am losing trailing zeroes on the data.
For example:
in the .CSV file, the data is this: 1250.80
in the .xlsx file, the data ends up being 1250.8
Seems minor but I need this to compare workbook to workbook and not see all of these diffs.
Here is the read routine
string[] csvInputfile = File.ReadAllLines(filePathName);
Here is how I am trying to put double quotes around the data
for (int idx = 0; idx < csvInputfile.Length; idx++)
{
csvInputfile[idx] = csvInputfile[idx].Replace(",", ",\"");
}
Here I am writing the contents to the Excel instance
for (int i = 0; i < csvInputfile.Length; i++)
{
ExcelWorksheet curWorksheet = excelPackage.Workbook.Worksheets.Add(csvInputfile[outerLoopCnt]);
for (int j = 0; j < csvInputfile.Length; j++)
{
innerLoopCnt++;
curWorksheet.Cells[j + 1, 1, j + 1, 100].Style.Numberformat.Format = "#";
curWorksheet.Cells[j + 1, 1].LoadFromText(csvInputfile[innerLoopCnt], format);
if (csvInputfile[innerLoopCnt + 1] == "")
{
outerLoopCnt = innerLoopCnt + 2;
innerLoopCnt += 2;
break;
}
}
if (innerLoopCnt == csvInputfile.Length)
{
break;
}
}
Here I save the file:
excelPackage.SaveAs(file);
This is the general idea and its not working very well. This is a crude approach but I'm not sure how else to make this work, I would appreciate any help

Can you set/reverse the starting index of the DataGridView rows?

I am creating a WPF form that requires a data table to represent different cell statuses using colors. I am using a DataGridView and creating cells depending on what file I decide to upload. The configuration for the data table is represented in 2 different types of files: a .xml and a .prd file. The .xml file is properly represented in the table since the rows and columns start at the correct point. The .prd file assigns the last row in the table as the first row.
eg. The .xml file has row[0] as row[0], but the .prd file has row[24] as row[0].
I need the DataGridView to not just represent the data, but also visually depict how the orientation is supposed to look. I just can't seem to figure out how to reverse the rows index.
Here is my code for the creation of the DataGridView for both types of files:
private void GenerateDataGridView(DataGridView dgv, int rows, int columns)
{
if (XMLSelected == true)
{
for (int j = 0; j <= rows; j++)
{
if (j == 0)
dgv.Columns.Add(null, null);
else
dgv.Columns.Add("rows" + j, "row " + j);
}
for (int i = 0; i <= columns; i++)
{
dgv.Rows.Add("column " + i);
}
XMLSelected = false;
}
else if(PRDSelected == true)
{
for (int j = 0; j <= rows; j++)
{
if (j == 0)
dgv.Columns.Add(null, null);
else
dgv.Columns.Add("rows" + j, "row " + j);
}
for (int i = 0; i <= columns; i++)
{
if (columns - i > 0)
{
dgv.Rows.Add("column " + (columns - i));
}
}
PRDSelected = false;
}
}
Just an FYI, the row headings are actually "columns 1,2,3..." and the column headings are "row 1, 2, 3..." so, for the .prd file, I need the "columns" to start from the highest value both in text and index from the top and decrease as it creates each row.

How to get the value of TableCell?

I have a GridView that I am trying to dump into a csv file, but when I run my code the cell values are not output; my output file only consists of commas and newlines, without the cell values
I've done a bunch of google searching and I am trying to print out the values row by row, cell by cell, using a nested for loop:
String output = String.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++) {
GridViewRow currentRow = GridView1.Rows[i];
for (int j = 0; j < currentRow.Cells.Count; j++) {
output += (currentRow.Cells[j].Text + ",");
}
output += Environment.NewLine;
}
Expected output should be a comma-separated list of values with multiple rows, but the file that is actually output looks similar to this:
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
etc
Thanks in advance for any help

Export Data Table To Excel

I am having a formatting issue when exporting my data table to Excel. The data is exported as it should, however if you look at my image, sometimes the cell height is increased and I am not sure why. I want the data to look the same from row to row. This is the syntax I am using to export
for (var i = 0; i < tbl.Columns.Count; i++)
workSheet.Cells[1, i + 1] = tbl.Columns[i].ColumnName;
for (var i = 0; i < tbl.Rows.Count; i++)
{
for (var j = 0; j < tbl.Columns.Count; j++)
workSheet.Cells[i + 2, j + 1] = tbl.Rows[i][j];
}
And here is an image that shows my formatting issues that I want to find a way to overcome. Can someone show me what syntax I need in order to have all row height/width the same?
Issue Image
EDIT
I tried this, but it throws an error and does not format as needed
The error is
System.Exception
Excel.Range range1 = workSheet.get_Range("A2", "S2000");
range1.EntireRow.Height.Value = 15;
You are over complicating it. Just use the UsedRange property. If workSheet is your actual variable name and 15 is the actual height you want to set, the below will work:
workSheet.UsedRange.EntireRow.RowHeight = 15;

DataGridView Copy Paste Multiple Columns

I was wondering if someone could help me. I have wrote some some code that will copy from the Clipboard into a DatagridView. The issue that I am having is that everything works as it is suppose to except the first cell.
For Example if I copy from Excel 2 columns only one row the value of 20 in the first column and the value of 30 in the second column
Then when I paste it in the DataGridView I get the following.
In the first column I get the number 20 and then a large space and the the value of 30 but in the second column only the value of 30 shows up.
Below is my code id anyone has an idea on how to fix that first cell please help me
if (e.Control && e.KeyCode == Keys.V)
{
char[] rowSplitter = { '\r', '\n' };
char[] columnSplitter = { '\t' };
//get the text from clipboard
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
//split it into lines
string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
//get the row and column of selected cell in grid
int r = DGV.SelectedCells[0].RowIndex;
int c = DGV.SelectedCells[0].ColumnIndex;
//add rows into grid to fit clipboard lines
if (DGV.Rows.Count < (r + rowsInClipboard.Length))
{
MessageBox.Show("PasteValues Will be Outside of Grid");
return;
}
// loop through the lines, split them into cells and place the values in the corresponding cell.
for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
{
//split row into cell values
string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
//cycle through cell values
for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
{
//assign cell value, only if it within columns of the grid
if (DGV.ColumnCount - 1 >= c + iCol)
{
DGV.Rows[r + iRow].Cells[c + iCol].Value = valuesInRow[iCol];
for (int i = 0;
i < selectedCellCount; i++)
{
DGV.SelectedCells[i].Value = valuesInRow[iCol]; ;
}
}
}
}
}

Categories

Resources