C# Excel 생성 및 쓰기
2658 단어 Excel
1 Microsoft.Office.Interop.Excel
2 using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
/// If the supplied excel File does not exist then Create it
/// </summary>
/// <param name="FileName"></param>
private void CreateExcelFile(string FileName)
{
//create
object Nothing = System.Reflection.Missing.Value;
var app = new Excel.Application();
app.Visible = false;
Excel.Workbook workBook = app.Workbooks.Add(Nothing);
Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];
worksheet.Name = "Work";
//headline
worksheet.Cells[1, 1] = "FileName";
worksheet.Cells[1, 2] = "FindString";
worksheet.Cells[1, 3] = "ReplaceString";
worksheet.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
workBook.Close(false, Type.Missing, Type.Missing);
app.Quit();
}
/// <summary>
/// open an excel file,then write the content to file
/// </summary>
/// <param name="FileName">file name</param>
/// <param name="findString">first cloumn</param>
/// <param name="replaceString">second cloumn</param>
private void WriteToExcel(string excelName,string filename,string findString,string replaceString)
{
//open
object Nothing = System.Reflection.Missing.Value;
var app = new Excel.Application();
app.Visible = false;
Excel.Workbook mybook = app.Workbooks.Open(excelName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets[1];
mysheet.Activate();
//get activate sheet max row count
int maxrow = mysheet.UsedRange.Rows.Count + 1;
mysheet.Cells[maxrow, 1] = filename;
mysheet.Cells[maxrow, 2] = findString;
mysheet.Cells[maxrow, 3] = replaceString;
mybook.Save();
mybook.Close(false, Type.Missing, Type.Missing);
mybook = null;
//quit excel app
app.Quit();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.