简体   繁体   中英

upload excel file and rename sheet to new sheet name using c# not use excel interop

I would like to upload excel file to server and this excel have only a sheet so when copy to server i want to change sheet name to a fixed name because user upload this file every months and sheet name that user set is by month too so i would like to rename file when upload to fixed ex: sheet name before upload is 'nov2015' when upload change name to 'data'

note: I use c# language to upload and rename file but not use excel interop library and i have code upload file below:

FolderPath = "~/Uploads/";
FileName = Path.GetFileName(fupload.PostedFile.FileName);
strFileType = Path.GetExtension(fupload.FileName).ToLower();

input_by = Session["user"].ToString();
FilePath = Server.MapPath(FolderPath + input_by + strFileType);
File.Delete(FilePath);
fupload.SaveAs(FilePath);

Consider using NPOI for this. You can change the worksheet name like -

Workbook workbook = new HSSFWorkbook();
Worksheet sheet = (Worksheet)workbook.Worksheets[index];
//Change the name of worksheet
sheet.Name = "NewWorksheetName";

For more details, visit NPOI at gitHub. You can also find variety of examples here on various usage of the tool -

https://github.com/tonyqus/npoi

Try using ClosedXML . It's a simple wrapper for the OpenXML SDK in C#. It should have no problem renaming a sheet, and the site has several examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM