I need to create one method that it's able to copy an excel sheet and then, paste it on a new excel sheet in the same workbook, but it's necessary to copy the formatting as well.
I found several codes, but all of them not copy the formatting, only the text.
below the code that I wrote:
// Opening Excel File
Microsoft.Office.Interop.Excel.Application excel = null;
excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(file, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
Microsoft.Office.Interop.Excel.Worksheet sheet2 = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(2);
// Copy the source sheet
Object defaultArg = Type.Missing;
sheet = (Worksheet)workbook.Sheets[1];
sheet.UsedRange.Copy(defaultArg);
// Paste on destination sheet
sheet2.UsedRange._PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
workbook.Save();
common.closeExcel(excel, workbook);
If anybody has any suggestion, please let me know.
Thanks,
This will make an exact copy of a sheet to a new sheet renamed as the original :
Dim x = 2
For numtimes = 1 To x
Sheet1.Copy _
After:=Sheet1
Next
Related
For my c# app, a user will input lines of remarks which will eventually be exported to an excel sheet. These remarks will be placed in a merged column cell(column I to L). The issue I am facing is that I always see a white space when the excel sheet is open as the contents are taking up too much vertical space. Only after manually resizing the height, then I am able to view the remarks within the cell.
I have tried these following : aRange.EntireColumn.AutoFit(); and worksheet.Columns.AutoFit(); and worksheet.Rows.AutoFit();
Excel.Workbook myExcelWorkbook;
Excel.Application xlsxApp = new Excel.Application();
Excel.Worksheet xlsht = new Excel.Worksheet();
File.Copy(AppDomain.CurrentDomain.BaseDirectory + #"checklist.xlsx", AppDomain.CurrentDomain.BaseDirectory + #"checklist_duplicate.xlsx", true);
string path = AppDomain.CurrentDomain.BaseDirectory + #"checklist_duplicate.xlsx;
xlsht = xlsxApp.Application.Workbooks.Open(path).Worksheets["Acceptance"];
// open the existing excel file //
myExcelWorkbook = (Excel.Workbook)(xlsxApp.Workbooks._Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing));
xlsht.Cells[13, "I"] = Savestate.one_one_one_grouping;/*Adding remarks into the merged column cells*/
How do I auto-size the height of these merged columns so these remarks can be seen?
I have the following code for creating and saving an excel file in c# but when it finishes, no file is created to my desktop, I can't figure out what I'm doing wrong:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = false;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
Range rangeAToC = ws.get_Range("A1", "C1");
string[] headerRow = { "GIP Id", "First Name", "Last Name"};
int indexAtRow = 0;
foreach (Range cell in rangeAToC)
{
cell.Value2 = headerRow[indexAtRow];
indexAtRow++;
}
//Save report
wb.SaveAs("C:/Users/Abdul/Desktop/GipEmployeeReport.xls", Type.Missing,
Type.Missing,Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//Close out COM objects
xlApp.Workbooks.Close();
xlApp.Quit();
first thing i noticed is that your using forward slashes (/) instead of backslashes \
Here's a problem description.
CONDITIONS:
General idea is to read a lot of real numbers from MS Excel file and put them inro ArrayList for further processing.
An excel workbook has only one worksheet. All the numbers are real and they are stored in one column.
I read these numbers row by row and put them into ArrayList.
PROBLEM: the process takes too much time. Program spends about 2 minutes to fill an ArrayList with 10000 elements.
Here's my code. I need your advise to make it faster. But the structure of the file cannot be modified. It's only possible to modify code.
Help me please to make it faster.
// Method GetExcelData opens 1 excel file, reads data row by row and adds
// it into the array of source Data Values (sourceDataValues in our case).
private void GetExcelData(string fullPath, ArrayList arrForValues)
{
Excel.Application excelapp = new Excel.Application();
excelapp.Visible = false;
// to avoid appearing of Excel window on the screen
Excel.Workbook excelappworkbook = excelapp.Workbooks.Open(
fullPath,
Type.Missing, Type.Missing, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Excel.Worksheet excelworksheet = (Excel.Worksheet)excelappworkbook.Worksheets.get_Item(1);
Excel.Range excelcells = excelworksheet.UsedRange;
uint rowsNum = 0;
for (rowsNum = 1; rowsNum != excelcells.Rows.Count; rowsNum++)
{
arrForValues.Add((excelcells.Cells[rowsNum, 1] as Excel.Range).Value2);
}
excelappworkbook.Close(false, Type.Missing, Type.Missing);
excelapp.Quit();
}
The problem is resolved.
Everything is qute simple. First, we read all the range of current worksheet into simple two-dimension array - worksheetValuesArray. After that we put all the values from that array into our container, converting the type of elements to double. Here's the part of corrected solution:
private void GetExcelData(string fullPath, List<double> arrForValues)
{
Excel.Application excelapp = new Excel.Application();
excelapp.Visible = false;
// to avoid appearing of Excel window on the screen
Excel.Workbook excelappworkbook = excelapp.Workbooks.Open(
fullPath,
Type.Missing, Type.Missing, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Excel.Worksheet excelworksheet = (Excel.Worksheet)excelappworkbook.Worksheets.get_Item(1);
Excel.Range excelcells = excelworksheet.UsedRange;
object[,] worksheetValuesArray = excelcells.get_Value(Type.Missing);
for (int col = 1; col < (worksheetValuesArray.GetLength(1)+1); col++)
{
for (int row = 1; row < (worksheetValuesArray.GetLength(0)+1); row++)
{
arrForValues.Add((double) worksheetValuesArray[row, col]);
}
}
excelappworkbook.Close(false, Type.Missing, Type.Missing);
excelapp.Quit();
}
My experience with Excel automation is that it is always slow. I usually try an alternative method, such as saving it as a CSV and reading the data with a stream reader and splitting the string on a delimiter (comma, tab, etc). I would suggest looking at the process of receiving your data and see if there is another format readily available.
I tweaked the for loop. See if this yields better results.
// Method GetExcelData opens 1 excel file, reads data row by row and adds
// it into the array of source Data Values (sourceDataValues in our case).
private void GetExcelData(string fullPath, ArrayList arrForValues)
{
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
excelapp.Visible = false;
// to avoid appearing of Excel window on the screen
Microsoft.Office.Interop.Excel.Workbook excelappworkbook = excelapp.Workbooks.Open(
fullPath,
Type.Missing, Type.Missing, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet excelworksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelappworkbook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range excelcells = excelworksheet.UsedRange;
Microsoft.Office.Interop.Excel.Range newRange = excelworksheet.get_Range("A1","A"+excelcells.Rows.Count);
object[,] items = newRange.Value;
for (int i = 1; i < items.Length; i++)
{
arrForValues.Add(items[i,1]);
}
excelappworkbook.Close(false, Type.Missing, Type.Missing);
excelapp.Quit();
}
I don't know if you're gonna find much more performance. Excel interop is just slow (due to marshaling across COM boundaries I assume). I have gained some performance in my code (especially in Excel 2007 and higher) by setting the following.
excelapp.ScreenUpdating = false;
and
excelapp.Calculation = Excel.XlCalculation.xlCalculationManual;
I am attempting to add a couple of new columns to an excel file. I can add the column pretty easily, but I can't figure out how to add a column header to the excel file. How on earth do you add a column header to a newly added column? Here is how I add 3 new columns to a An excel worksheet.
Range rng = (Range)wkSheet.get_Range("A1", Type.Missing);
rng.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
Range rng2 = (Range)wkSheet.get_Range("A1", Type.Missing);
rng2.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
Range rng3 = (Range)wkSheet.get_Range("A1", Type.Missing);
rng3.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
wkSheet.SaveAs("C:\\Code\\UPMC\\ISD\\EADIS\\UPMC.ISD.EADIS.ACO.ACOBackLoad\\App_Data\\test", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
You can write to any cell like so (this just writes to the "header" as an example):
// Type _ = Type.Missing;
wkSheet.Range["A1", _].Value2 = "Heading 1";
wkSheet.Range["B1", _].Value2 = "Heading 2";
wkSheet.Range["C1", _].Value2 = "Heading 3";
I have an Excel file with 5 worksheets and I want with c# code to open it
and when it is opened I want the sheet number 3 to be activated.
How can I do that?
Like this:
using Excel;
Excel.Application excelApp = new Excel.ApplicationClass();
// if you want to make excel visible to user, set this property to true, false by default
excelApp.Visible = true;
// open an existing workbook
string workbookPath = "c:/SomeWorkBook.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
// get all sheets in workbook
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
// get some sheet
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet =
(Excel.Worksheet)excelSheets.get_Item(currentSheet);
// access cell within sheet
Excel.Range excelCell =
(Excel.Range)excelWorksheet.get_Range("A1", "A1");
Hope this helps
MDSN info here
What about something like this: (untested)
//using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass app = new Excel.ApplicationClass();
Excel.Workbook workbook = app.Workbooks.Open("YourFile.xls",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets["Number 3"];
worksheet.Activate();
If wanting to present visual feedback to the User, these two statements will set the activated sheet and select the range accordingly:
Consider including the following statement immediately prior to initializing the Excel.Range...
// Set Active sheet in Excel
excelWorksheet.Activate()
Also consider the following statement immediately after initializing the Excel.Range...
// Set Active range in Excel
excelCell.Activate()
public static Workbook openExternalWorkBook(String fileName)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
return excel.Workbooks.Open(fileName, false);
}